Coverage Report

Created: 2025-11-24 06:11

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
56.3k
{
27
56.3k
    return bytes___bytes___impl((PyBytesObject *)self);
28
56.3k
}
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
3.28M
{
53
3.28M
    PyObject *return_value = NULL;
54
3.28M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
55
56
3.28M
    #define NUM_KEYWORDS 2
57
3.28M
    static struct {
58
3.28M
        PyGC_Head _this_is_not_used;
59
3.28M
        PyObject_VAR_HEAD
60
3.28M
        Py_hash_t ob_hash;
61
3.28M
        PyObject *ob_item[NUM_KEYWORDS];
62
3.28M
    } _kwtuple = {
63
3.28M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
64
3.28M
        .ob_hash = -1,
65
3.28M
        .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
66
3.28M
    };
67
3.28M
    #undef NUM_KEYWORDS
68
3.28M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
69
70
    #else  // !Py_BUILD_CORE
71
    #  define KWTUPLE NULL
72
    #endif  // !Py_BUILD_CORE
73
74
3.28M
    static const char * const _keywords[] = {"sep", "maxsplit", NULL};
75
3.28M
    static _PyArg_Parser _parser = {
76
3.28M
        .keywords = _keywords,
77
3.28M
        .fname = "split",
78
3.28M
        .kwtuple = KWTUPLE,
79
3.28M
    };
80
3.28M
    #undef KWTUPLE
81
3.28M
    PyObject *argsbuf[2];
82
3.28M
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
83
3.28M
    PyObject *sep = Py_None;
84
3.28M
    Py_ssize_t maxsplit = -1;
85
86
3.28M
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
87
3.28M
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
88
3.28M
    if (!args) {
89
0
        goto exit;
90
0
    }
91
3.28M
    if (!noptargs) {
92
0
        goto skip_optional_pos;
93
0
    }
94
3.28M
    if (args[0]) {
95
3.28M
        sep = args[0];
96
3.28M
        if (!--noptargs) {
97
3.28M
            goto skip_optional_pos;
98
3.28M
        }
99
3.28M
    }
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
3.28M
skip_optional_pos:
113
3.28M
    return_value = bytes_split_impl((PyBytesObject *)self, sep, maxsplit);
114
115
3.28M
exit:
116
3.28M
    return return_value;
117
3.28M
}
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
0
{
141
0
    PyObject *return_value = NULL;
142
0
    Py_buffer sep = {NULL, NULL};
143
144
0
    if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
145
0
        goto exit;
146
0
    }
147
0
    return_value = bytes_partition_impl((PyBytesObject *)self, &sep);
148
149
0
exit:
150
    /* Cleanup for sep */
151
0
    if (sep.obj) {
152
0
       PyBuffer_Release(&sep);
153
0
    }
154
155
0
    return return_value;
156
0
}
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
8.87k
{
309
8.87k
    PyObject *return_value = NULL;
310
311
8.87k
    return_value = bytes_join_impl((PyBytesObject *)self, iterable_of_bytes);
312
313
8.87k
    return return_value;
314
8.87k
}
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
4.02M
{
339
4.02M
    PyObject *return_value = NULL;
340
4.02M
    PyObject *sub;
341
4.02M
    Py_ssize_t start = 0;
342
4.02M
    Py_ssize_t end = PY_SSIZE_T_MAX;
343
344
4.02M
    if (!_PyArg_CheckPositional("find", nargs, 1, 3)) {
345
0
        goto exit;
346
0
    }
347
4.02M
    sub = args[0];
348
4.02M
    if (nargs < 2) {
349
4.02M
        goto skip_optional;
350
4.02M
    }
351
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
352
0
        goto exit;
353
0
    }
354
0
    if (nargs < 3) {
355
0
        goto skip_optional;
356
0
    }
357
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
358
0
        goto exit;
359
0
    }
360
4.02M
skip_optional:
361
4.02M
    return_value = bytes_find_impl((PyBytesObject *)self, sub, start, end);
362
363
4.02M
exit:
364
4.02M
    return return_value;
365
4.02M
}
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
25.7k
{
441
25.7k
    PyObject *return_value = NULL;
442
25.7k
    PyObject *sub;
443
25.7k
    Py_ssize_t start = 0;
444
25.7k
    Py_ssize_t end = PY_SSIZE_T_MAX;
445
446
25.7k
    if (!_PyArg_CheckPositional("rfind", nargs, 1, 3)) {
447
0
        goto exit;
448
0
    }
449
25.7k
    sub = args[0];
450
25.7k
    if (nargs < 2) {
451
25.7k
        goto skip_optional;
452
25.7k
    }
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
25.7k
skip_optional:
463
25.7k
    return_value = bytes_rfind_impl((PyBytesObject *)self, sub, start, end);
464
465
25.7k
exit:
466
25.7k
    return return_value;
467
25.7k
}
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
0
{
605
0
    PyObject *return_value = NULL;
606
0
    PyObject *bytes = Py_None;
607
608
0
    if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
609
0
        goto exit;
610
0
    }
611
0
    if (nargs < 1) {
612
0
        goto skip_optional;
613
0
    }
614
0
    bytes = args[0];
615
0
skip_optional:
616
0
    return_value = bytes_rstrip_impl((PyBytesObject *)self, bytes);
617
618
0
exit:
619
0
    return return_value;
620
0
}
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
4.95M
{
643
4.95M
    PyObject *return_value = NULL;
644
4.95M
    PyObject *sub;
645
4.95M
    Py_ssize_t start = 0;
646
4.95M
    Py_ssize_t end = PY_SSIZE_T_MAX;
647
648
4.95M
    if (!_PyArg_CheckPositional("count", nargs, 1, 3)) {
649
0
        goto exit;
650
0
    }
651
4.95M
    sub = args[0];
652
4.95M
    if (nargs < 2) {
653
4.95M
        goto skip_optional;
654
4.95M
    }
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
4.95M
skip_optional:
665
4.95M
    return_value = bytes_count_impl((PyBytesObject *)self, sub, start, end);
666
667
4.95M
exit:
668
4.95M
    return return_value;
669
4.95M
}
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
28
{
763
28
    PyObject *return_value = NULL;
764
28
    Py_buffer frm = {NULL, NULL};
765
28
    Py_buffer to = {NULL, NULL};
766
767
28
    if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
768
0
        goto exit;
769
0
    }
770
28
    if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
771
0
        goto exit;
772
0
    }
773
28
    if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
774
0
        goto exit;
775
0
    }
776
28
    return_value = bytes_maketrans_impl(&frm, &to);
777
778
28
exit:
779
    /* Cleanup for frm */
780
28
    if (frm.obj) {
781
28
       PyBuffer_Release(&frm);
782
28
    }
783
    /* Cleanup for to */
784
28
    if (to.obj) {
785
28
       PyBuffer_Release(&to);
786
28
    }
787
788
28
    return return_value;
789
28
}
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 the optional argument count is given, only the first count occurrences are\n"
802
"replaced.");
803
804
#define BYTES_REPLACE_METHODDEF    \
805
    {"replace", _PyCFunction_CAST(bytes_replace), METH_FASTCALL, 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)
813
46.7k
{
814
46.7k
    PyObject *return_value = NULL;
815
46.7k
    Py_buffer old = {NULL, NULL};
816
46.7k
    Py_buffer new = {NULL, NULL};
817
46.7k
    Py_ssize_t count = -1;
818
819
46.7k
    if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
820
0
        goto exit;
821
0
    }
822
46.7k
    if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
823
0
        goto exit;
824
0
    }
825
46.7k
    if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
826
0
        goto exit;
827
0
    }
828
46.7k
    if (nargs < 3) {
829
46.7k
        goto skip_optional;
830
46.7k
    }
831
0
    {
832
0
        Py_ssize_t ival = -1;
833
0
        PyObject *iobj = _PyNumber_Index(args[2]);
834
0
        if (iobj != NULL) {
835
0
            ival = PyLong_AsSsize_t(iobj);
836
0
            Py_DECREF(iobj);
837
0
        }
838
0
        if (ival == -1 && PyErr_Occurred()) {
839
0
            goto exit;
840
0
        }
841
0
        count = ival;
842
0
    }
843
46.7k
skip_optional:
844
46.7k
    return_value = bytes_replace_impl((PyBytesObject *)self, &old, &new, count);
845
846
46.7k
exit:
847
    /* Cleanup for old */
848
46.7k
    if (old.obj) {
849
46.7k
       PyBuffer_Release(&old);
850
46.7k
    }
851
    /* Cleanup for new */
852
46.7k
    if (new.obj) {
853
46.7k
       PyBuffer_Release(&new);
854
46.7k
    }
855
856
46.7k
    return return_value;
857
46.7k
}
858
859
PyDoc_STRVAR(bytes_removeprefix__doc__,
860
"removeprefix($self, prefix, /)\n"
861
"--\n"
862
"\n"
863
"Return a bytes object with the given prefix string removed if present.\n"
864
"\n"
865
"If the bytes starts with the prefix string, return bytes[len(prefix):].\n"
866
"Otherwise, return a copy of the original bytes.");
867
868
#define BYTES_REMOVEPREFIX_METHODDEF    \
869
    {"removeprefix", (PyCFunction)bytes_removeprefix, METH_O, bytes_removeprefix__doc__},
870
871
static PyObject *
872
bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix);
873
874
static PyObject *
875
bytes_removeprefix(PyObject *self, PyObject *arg)
876
0
{
877
0
    PyObject *return_value = NULL;
878
0
    Py_buffer prefix = {NULL, NULL};
879
880
0
    if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
881
0
        goto exit;
882
0
    }
883
0
    return_value = bytes_removeprefix_impl((PyBytesObject *)self, &prefix);
884
885
0
exit:
886
    /* Cleanup for prefix */
887
0
    if (prefix.obj) {
888
0
       PyBuffer_Release(&prefix);
889
0
    }
890
891
0
    return return_value;
892
0
}
893
894
PyDoc_STRVAR(bytes_removesuffix__doc__,
895
"removesuffix($self, suffix, /)\n"
896
"--\n"
897
"\n"
898
"Return a bytes object with the given suffix string removed if present.\n"
899
"\n"
900
"If the bytes ends with the suffix string and that suffix is not empty,\n"
901
"return bytes[:-len(prefix)].  Otherwise, return a copy of the original\n"
902
"bytes.");
903
904
#define BYTES_REMOVESUFFIX_METHODDEF    \
905
    {"removesuffix", (PyCFunction)bytes_removesuffix, METH_O, bytes_removesuffix__doc__},
906
907
static PyObject *
908
bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix);
909
910
static PyObject *
911
bytes_removesuffix(PyObject *self, PyObject *arg)
912
0
{
913
0
    PyObject *return_value = NULL;
914
0
    Py_buffer suffix = {NULL, NULL};
915
916
0
    if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
917
0
        goto exit;
918
0
    }
919
0
    return_value = bytes_removesuffix_impl((PyBytesObject *)self, &suffix);
920
921
0
exit:
922
    /* Cleanup for suffix */
923
0
    if (suffix.obj) {
924
0
       PyBuffer_Release(&suffix);
925
0
    }
926
927
0
    return return_value;
928
0
}
929
930
PyDoc_STRVAR(bytes_startswith__doc__,
931
"startswith($self, prefix[, start[, end]], /)\n"
932
"--\n"
933
"\n"
934
"Return True if the bytes starts with the specified prefix, False otherwise.\n"
935
"\n"
936
"  prefix\n"
937
"    A bytes or a tuple of bytes to try.\n"
938
"  start\n"
939
"    Optional start position. Default: start of the bytes.\n"
940
"  end\n"
941
"    Optional stop position. Default: end of the bytes.");
942
943
#define BYTES_STARTSWITH_METHODDEF    \
944
    {"startswith", _PyCFunction_CAST(bytes_startswith), METH_FASTCALL, bytes_startswith__doc__},
945
946
static PyObject *
947
bytes_startswith_impl(PyBytesObject *self, PyObject *subobj,
948
                      Py_ssize_t start, Py_ssize_t end);
949
950
static PyObject *
951
bytes_startswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
952
398k
{
953
398k
    PyObject *return_value = NULL;
954
398k
    PyObject *subobj;
955
398k
    Py_ssize_t start = 0;
956
398k
    Py_ssize_t end = PY_SSIZE_T_MAX;
957
958
398k
    if (!_PyArg_CheckPositional("startswith", nargs, 1, 3)) {
959
0
        goto exit;
960
0
    }
961
398k
    subobj = args[0];
962
398k
    if (nargs < 2) {
963
398k
        goto skip_optional;
964
398k
    }
965
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
966
0
        goto exit;
967
0
    }
968
0
    if (nargs < 3) {
969
0
        goto skip_optional;
970
0
    }
971
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
972
0
        goto exit;
973
0
    }
974
398k
skip_optional:
975
398k
    return_value = bytes_startswith_impl((PyBytesObject *)self, subobj, start, end);
976
977
398k
exit:
978
398k
    return return_value;
979
398k
}
980
981
PyDoc_STRVAR(bytes_endswith__doc__,
982
"endswith($self, suffix[, start[, end]], /)\n"
983
"--\n"
984
"\n"
985
"Return True if the bytes ends with the specified suffix, False otherwise.\n"
986
"\n"
987
"  suffix\n"
988
"    A bytes or a tuple of bytes to try.\n"
989
"  start\n"
990
"    Optional start position. Default: start of the bytes.\n"
991
"  end\n"
992
"    Optional stop position. Default: end of the bytes.");
993
994
#define BYTES_ENDSWITH_METHODDEF    \
995
    {"endswith", _PyCFunction_CAST(bytes_endswith), METH_FASTCALL, bytes_endswith__doc__},
996
997
static PyObject *
998
bytes_endswith_impl(PyBytesObject *self, PyObject *subobj, Py_ssize_t start,
999
                    Py_ssize_t end);
1000
1001
static PyObject *
1002
bytes_endswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1003
0
{
1004
0
    PyObject *return_value = NULL;
1005
0
    PyObject *subobj;
1006
0
    Py_ssize_t start = 0;
1007
0
    Py_ssize_t end = PY_SSIZE_T_MAX;
1008
1009
0
    if (!_PyArg_CheckPositional("endswith", nargs, 1, 3)) {
1010
0
        goto exit;
1011
0
    }
1012
0
    subobj = args[0];
1013
0
    if (nargs < 2) {
1014
0
        goto skip_optional;
1015
0
    }
1016
0
    if (!_PyEval_SliceIndex(args[1], &start)) {
1017
0
        goto exit;
1018
0
    }
1019
0
    if (nargs < 3) {
1020
0
        goto skip_optional;
1021
0
    }
1022
0
    if (!_PyEval_SliceIndex(args[2], &end)) {
1023
0
        goto exit;
1024
0
    }
1025
0
skip_optional:
1026
0
    return_value = bytes_endswith_impl((PyBytesObject *)self, subobj, start, end);
1027
1028
0
exit:
1029
0
    return return_value;
1030
0
}
1031
1032
PyDoc_STRVAR(bytes_decode__doc__,
1033
"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
1034
"--\n"
1035
"\n"
1036
"Decode the bytes using the codec registered for encoding.\n"
1037
"\n"
1038
"  encoding\n"
1039
"    The encoding with which to decode the bytes.\n"
1040
"  errors\n"
1041
"    The error handling scheme to use for the handling of decoding errors.\n"
1042
"    The default is \'strict\' meaning that decoding errors raise a\n"
1043
"    UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
1044
"    as well as any other name registered with codecs.register_error that\n"
1045
"    can handle UnicodeDecodeErrors.");
1046
1047
#define BYTES_DECODE_METHODDEF    \
1048
    {"decode", _PyCFunction_CAST(bytes_decode), METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__},
1049
1050
static PyObject *
1051
bytes_decode_impl(PyBytesObject *self, const char *encoding,
1052
                  const char *errors);
1053
1054
static PyObject *
1055
bytes_decode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1056
8.25M
{
1057
8.25M
    PyObject *return_value = NULL;
1058
8.25M
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1059
1060
8.25M
    #define NUM_KEYWORDS 2
1061
8.25M
    static struct {
1062
8.25M
        PyGC_Head _this_is_not_used;
1063
8.25M
        PyObject_VAR_HEAD
1064
8.25M
        Py_hash_t ob_hash;
1065
8.25M
        PyObject *ob_item[NUM_KEYWORDS];
1066
8.25M
    } _kwtuple = {
1067
8.25M
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1068
8.25M
        .ob_hash = -1,
1069
8.25M
        .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
1070
8.25M
    };
1071
8.25M
    #undef NUM_KEYWORDS
1072
8.25M
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1073
1074
    #else  // !Py_BUILD_CORE
1075
    #  define KWTUPLE NULL
1076
    #endif  // !Py_BUILD_CORE
1077
1078
8.25M
    static const char * const _keywords[] = {"encoding", "errors", NULL};
1079
8.25M
    static _PyArg_Parser _parser = {
1080
8.25M
        .keywords = _keywords,
1081
8.25M
        .fname = "decode",
1082
8.25M
        .kwtuple = KWTUPLE,
1083
8.25M
    };
1084
8.25M
    #undef KWTUPLE
1085
8.25M
    PyObject *argsbuf[2];
1086
8.25M
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1087
8.25M
    const char *encoding = NULL;
1088
8.25M
    const char *errors = NULL;
1089
1090
8.25M
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1091
8.25M
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1092
8.25M
    if (!args) {
1093
0
        goto exit;
1094
0
    }
1095
8.25M
    if (!noptargs) {
1096
44.7k
        goto skip_optional_pos;
1097
44.7k
    }
1098
8.20M
    if (args[0]) {
1099
8.20M
        if (!PyUnicode_Check(args[0])) {
1100
0
            _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
1101
0
            goto exit;
1102
0
        }
1103
8.20M
        Py_ssize_t encoding_length;
1104
8.20M
        encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
1105
8.20M
        if (encoding == NULL) {
1106
0
            goto exit;
1107
0
        }
1108
8.20M
        if (strlen(encoding) != (size_t)encoding_length) {
1109
2.12k
            PyErr_SetString(PyExc_ValueError, "embedded null character");
1110
2.12k
            goto exit;
1111
2.12k
        }
1112
8.20M
        if (!--noptargs) {
1113
470k
            goto skip_optional_pos;
1114
470k
        }
1115
8.20M
    }
1116
7.73M
    if (!PyUnicode_Check(args[1])) {
1117
0
        _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
1118
0
        goto exit;
1119
0
    }
1120
7.73M
    Py_ssize_t errors_length;
1121
7.73M
    errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1122
7.73M
    if (errors == NULL) {
1123
0
        goto exit;
1124
0
    }
1125
7.73M
    if (strlen(errors) != (size_t)errors_length) {
1126
0
        PyErr_SetString(PyExc_ValueError, "embedded null character");
1127
0
        goto exit;
1128
0
    }
1129
8.25M
skip_optional_pos:
1130
8.25M
    return_value = bytes_decode_impl((PyBytesObject *)self, encoding, errors);
1131
1132
8.25M
exit:
1133
8.25M
    return return_value;
1134
8.25M
}
1135
1136
PyDoc_STRVAR(bytes_splitlines__doc__,
1137
"splitlines($self, /, keepends=False)\n"
1138
"--\n"
1139
"\n"
1140
"Return a list of the lines in the bytes, breaking at line boundaries.\n"
1141
"\n"
1142
"Line breaks are not included in the resulting list unless keepends is given and\n"
1143
"true.");
1144
1145
#define BYTES_SPLITLINES_METHODDEF    \
1146
    {"splitlines", _PyCFunction_CAST(bytes_splitlines), METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__},
1147
1148
static PyObject *
1149
bytes_splitlines_impl(PyBytesObject *self, int keepends);
1150
1151
static PyObject *
1152
bytes_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1153
0
{
1154
0
    PyObject *return_value = NULL;
1155
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1156
1157
0
    #define NUM_KEYWORDS 1
1158
0
    static struct {
1159
0
        PyGC_Head _this_is_not_used;
1160
0
        PyObject_VAR_HEAD
1161
0
        Py_hash_t ob_hash;
1162
0
        PyObject *ob_item[NUM_KEYWORDS];
1163
0
    } _kwtuple = {
1164
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1165
0
        .ob_hash = -1,
1166
0
        .ob_item = { &_Py_ID(keepends), },
1167
0
    };
1168
0
    #undef NUM_KEYWORDS
1169
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1170
1171
    #else  // !Py_BUILD_CORE
1172
    #  define KWTUPLE NULL
1173
    #endif  // !Py_BUILD_CORE
1174
1175
0
    static const char * const _keywords[] = {"keepends", NULL};
1176
0
    static _PyArg_Parser _parser = {
1177
0
        .keywords = _keywords,
1178
0
        .fname = "splitlines",
1179
0
        .kwtuple = KWTUPLE,
1180
0
    };
1181
0
    #undef KWTUPLE
1182
0
    PyObject *argsbuf[1];
1183
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1184
0
    int keepends = 0;
1185
1186
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1187
0
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1188
0
    if (!args) {
1189
0
        goto exit;
1190
0
    }
1191
0
    if (!noptargs) {
1192
0
        goto skip_optional_pos;
1193
0
    }
1194
0
    keepends = PyObject_IsTrue(args[0]);
1195
0
    if (keepends < 0) {
1196
0
        goto exit;
1197
0
    }
1198
0
skip_optional_pos:
1199
0
    return_value = bytes_splitlines_impl((PyBytesObject *)self, keepends);
1200
1201
0
exit:
1202
0
    return return_value;
1203
0
}
1204
1205
PyDoc_STRVAR(bytes_fromhex__doc__,
1206
"fromhex($type, string, /)\n"
1207
"--\n"
1208
"\n"
1209
"Create a bytes object from a string of hexadecimal numbers.\n"
1210
"\n"
1211
"Spaces between two numbers are accepted.\n"
1212
"Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'.");
1213
1214
#define BYTES_FROMHEX_METHODDEF    \
1215
    {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__},
1216
1217
static PyObject *
1218
bytes_fromhex_impl(PyTypeObject *type, PyObject *string);
1219
1220
static PyObject *
1221
bytes_fromhex(PyObject *type, PyObject *string)
1222
45.7k
{
1223
45.7k
    PyObject *return_value = NULL;
1224
1225
45.7k
    return_value = bytes_fromhex_impl((PyTypeObject *)type, string);
1226
1227
45.7k
    return return_value;
1228
45.7k
}
1229
1230
PyDoc_STRVAR(bytes_hex__doc__,
1231
"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
1232
"--\n"
1233
"\n"
1234
"Create a string of hexadecimal numbers from a bytes object.\n"
1235
"\n"
1236
"  sep\n"
1237
"    An optional single character or byte to separate hex bytes.\n"
1238
"  bytes_per_sep\n"
1239
"    How many bytes between separators.  Positive values count from the\n"
1240
"    right, negative values count from the left.\n"
1241
"\n"
1242
"Example:\n"
1243
">>> value = b\'\\xb9\\x01\\xef\'\n"
1244
">>> value.hex()\n"
1245
"\'b901ef\'\n"
1246
">>> value.hex(\':\')\n"
1247
"\'b9:01:ef\'\n"
1248
">>> value.hex(\':\', 2)\n"
1249
"\'b9:01ef\'\n"
1250
">>> value.hex(\':\', -2)\n"
1251
"\'b901:ef\'");
1252
1253
#define BYTES_HEX_METHODDEF    \
1254
    {"hex", _PyCFunction_CAST(bytes_hex), METH_FASTCALL|METH_KEYWORDS, bytes_hex__doc__},
1255
1256
static PyObject *
1257
bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep);
1258
1259
static PyObject *
1260
bytes_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1261
0
{
1262
0
    PyObject *return_value = NULL;
1263
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1264
1265
0
    #define NUM_KEYWORDS 2
1266
0
    static struct {
1267
0
        PyGC_Head _this_is_not_used;
1268
0
        PyObject_VAR_HEAD
1269
0
        Py_hash_t ob_hash;
1270
0
        PyObject *ob_item[NUM_KEYWORDS];
1271
0
    } _kwtuple = {
1272
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1273
0
        .ob_hash = -1,
1274
0
        .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
1275
0
    };
1276
0
    #undef NUM_KEYWORDS
1277
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1278
1279
    #else  // !Py_BUILD_CORE
1280
    #  define KWTUPLE NULL
1281
    #endif  // !Py_BUILD_CORE
1282
1283
0
    static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
1284
0
    static _PyArg_Parser _parser = {
1285
0
        .keywords = _keywords,
1286
0
        .fname = "hex",
1287
0
        .kwtuple = KWTUPLE,
1288
0
    };
1289
0
    #undef KWTUPLE
1290
0
    PyObject *argsbuf[2];
1291
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1292
0
    PyObject *sep = NULL;
1293
0
    int bytes_per_sep = 1;
1294
1295
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1296
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1297
0
    if (!args) {
1298
0
        goto exit;
1299
0
    }
1300
0
    if (!noptargs) {
1301
0
        goto skip_optional_pos;
1302
0
    }
1303
0
    if (args[0]) {
1304
0
        sep = args[0];
1305
0
        if (!--noptargs) {
1306
0
            goto skip_optional_pos;
1307
0
        }
1308
0
    }
1309
0
    bytes_per_sep = PyLong_AsInt(args[1]);
1310
0
    if (bytes_per_sep == -1 && PyErr_Occurred()) {
1311
0
        goto exit;
1312
0
    }
1313
0
skip_optional_pos:
1314
0
    return_value = bytes_hex_impl((PyBytesObject *)self, sep, bytes_per_sep);
1315
1316
0
exit:
1317
0
    return return_value;
1318
0
}
1319
1320
static PyObject *
1321
bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
1322
               const char *errors);
1323
1324
static PyObject *
1325
bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1326
801k
{
1327
801k
    PyObject *return_value = NULL;
1328
801k
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1329
1330
801k
    #define NUM_KEYWORDS 3
1331
801k
    static struct {
1332
801k
        PyGC_Head _this_is_not_used;
1333
801k
        PyObject_VAR_HEAD
1334
801k
        Py_hash_t ob_hash;
1335
801k
        PyObject *ob_item[NUM_KEYWORDS];
1336
801k
    } _kwtuple = {
1337
801k
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1338
801k
        .ob_hash = -1,
1339
801k
        .ob_item = { &_Py_ID(source), &_Py_ID(encoding), &_Py_ID(errors), },
1340
801k
    };
1341
801k
    #undef NUM_KEYWORDS
1342
801k
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1343
1344
    #else  // !Py_BUILD_CORE
1345
    #  define KWTUPLE NULL
1346
    #endif  // !Py_BUILD_CORE
1347
1348
801k
    static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
1349
801k
    static _PyArg_Parser _parser = {
1350
801k
        .keywords = _keywords,
1351
801k
        .fname = "bytes",
1352
801k
        .kwtuple = KWTUPLE,
1353
801k
    };
1354
801k
    #undef KWTUPLE
1355
801k
    PyObject *argsbuf[3];
1356
801k
    PyObject * const *fastargs;
1357
801k
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1358
801k
    Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
1359
801k
    PyObject *x = NULL;
1360
801k
    const char *encoding = NULL;
1361
801k
    const char *errors = NULL;
1362
1363
801k
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
1364
801k
            /*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1365
801k
    if (!fastargs) {
1366
0
        goto exit;
1367
0
    }
1368
801k
    if (!noptargs) {
1369
0
        goto skip_optional_pos;
1370
0
    }
1371
801k
    if (fastargs[0]) {
1372
801k
        x = fastargs[0];
1373
801k
        if (!--noptargs) {
1374
547k
            goto skip_optional_pos;
1375
547k
        }
1376
801k
    }
1377
253k
    if (fastargs[1]) {
1378
253k
        if (!PyUnicode_Check(fastargs[1])) {
1379
0
            _PyArg_BadArgument("bytes", "argument 'encoding'", "str", fastargs[1]);
1380
0
            goto exit;
1381
0
        }
1382
253k
        Py_ssize_t encoding_length;
1383
253k
        encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
1384
253k
        if (encoding == NULL) {
1385
0
            goto exit;
1386
0
        }
1387
253k
        if (strlen(encoding) != (size_t)encoding_length) {
1388
0
            PyErr_SetString(PyExc_ValueError, "embedded null character");
1389
0
            goto exit;
1390
0
        }
1391
253k
        if (!--noptargs) {
1392
253k
            goto skip_optional_pos;
1393
253k
        }
1394
253k
    }
1395
0
    if (!PyUnicode_Check(fastargs[2])) {
1396
0
        _PyArg_BadArgument("bytes", "argument 'errors'", "str", fastargs[2]);
1397
0
        goto exit;
1398
0
    }
1399
0
    Py_ssize_t errors_length;
1400
0
    errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
1401
0
    if (errors == NULL) {
1402
0
        goto exit;
1403
0
    }
1404
0
    if (strlen(errors) != (size_t)errors_length) {
1405
0
        PyErr_SetString(PyExc_ValueError, "embedded null character");
1406
0
        goto exit;
1407
0
    }
1408
801k
skip_optional_pos:
1409
801k
    return_value = bytes_new_impl(type, x, encoding, errors);
1410
1411
801k
exit:
1412
801k
    return return_value;
1413
801k
}
1414
/*[clinic end generated code: output=08b9507244f73638 input=a9049054013a1b77]*/