Debian Codegraph @

Objects/clinic/bytesobject.c.h

raw
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 {
27 return bytes___bytes___impl((PyBytesObject *)self);
28 }
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 {
53 PyObject *return_value = NULL;
54 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
55
56 #define NUM_KEYWORDS 2
57 static struct {
58 PyGC_Head _this_is_not_used;
59 PyObject_VAR_HEAD
60 Py_hash_t ob_hash;
61 PyObject *ob_item[NUM_KEYWORDS];
62 } _kwtuple = {
63 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
64 .ob_hash = -1,
65 .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
66 };
67 #undef NUM_KEYWORDS
68 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
69
70 #else // !Py_BUILD_CORE
71 # define KWTUPLE NULL
72 #endif // !Py_BUILD_CORE
73
74 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
75 static _PyArg_Parser _parser = {
76 .keywords = _keywords,
77 .fname = "split",
78 .kwtuple = KWTUPLE,
79 };
80 #undef KWTUPLE
81 PyObject *argsbuf[2];
82 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
83 PyObject *sep = Py_None;
84 Py_ssize_t maxsplit = -1;
85
86 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
87 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
88 if (!args) {
89 goto exit;
90 }
91 if (!noptargs) {
92 goto skip_optional_pos;
93 }
94 if (args[0]) {
95 sep = args[0];
96 if (!--noptargs) {
97 goto skip_optional_pos;
98 }
99 }
100 {
101 Py_ssize_t ival = -1;
102 PyObject *iobj = _PyNumber_Index(args[1]);
103 if (iobj != NULL) {
104 ival = PyLong_AsSsize_t(iobj);
105 Py_DECREF(iobj);
106 }
107 if (ival == -1 && PyErr_Occurred()) {
108 goto exit;
109 }
110 maxsplit = ival;
111 }
112 skip_optional_pos:
113 return_value = bytes_split_impl((PyBytesObject *)self, sep, maxsplit);
114
115 exit:
116 return return_value;
117 }
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 {
141 PyObject *return_value = NULL;
142 Py_buffer sep = {NULL, NULL};
143
144 if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
145 goto exit;
146 }
147 return_value = bytes_partition_impl((PyBytesObject *)self, &sep);
148
149 exit:
150 /* Cleanup for sep */
151 if (sep.obj) {
152 PyBuffer_Release(&sep);
153 }
154
155 return return_value;
156 }
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 {
180 PyObject *return_value = NULL;
181 Py_buffer sep = {NULL, NULL};
182
183 if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
184 goto exit;
185 }
186 return_value = bytes_rpartition_impl((PyBytesObject *)self, &sep);
187
188 exit:
189 /* Cleanup for sep */
190 if (sep.obj) {
191 PyBuffer_Release(&sep);
192 }
193
194 return return_value;
195 }
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 {
222 PyObject *return_value = NULL;
223 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
224
225 #define NUM_KEYWORDS 2
226 static struct {
227 PyGC_Head _this_is_not_used;
228 PyObject_VAR_HEAD
229 Py_hash_t ob_hash;
230 PyObject *ob_item[NUM_KEYWORDS];
231 } _kwtuple = {
232 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
233 .ob_hash = -1,
234 .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
235 };
236 #undef NUM_KEYWORDS
237 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
238
239 #else // !Py_BUILD_CORE
240 # define KWTUPLE NULL
241 #endif // !Py_BUILD_CORE
242
243 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
244 static _PyArg_Parser _parser = {
245 .keywords = _keywords,
246 .fname = "rsplit",
247 .kwtuple = KWTUPLE,
248 };
249 #undef KWTUPLE
250 PyObject *argsbuf[2];
251 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
252 PyObject *sep = Py_None;
253 Py_ssize_t maxsplit = -1;
254
255 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
256 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
257 if (!args) {
258 goto exit;
259 }
260 if (!noptargs) {
261 goto skip_optional_pos;
262 }
263 if (args[0]) {
264 sep = args[0];
265 if (!--noptargs) {
266 goto skip_optional_pos;
267 }
268 }
269 {
270 Py_ssize_t ival = -1;
271 PyObject *iobj = _PyNumber_Index(args[1]);
272 if (iobj != NULL) {
273 ival = PyLong_AsSsize_t(iobj);
274 Py_DECREF(iobj);
275 }
276 if (ival == -1 && PyErr_Occurred()) {
277 goto exit;
278 }
279 maxsplit = ival;
280 }
281 skip_optional_pos:
282 return_value = bytes_rsplit_impl((PyBytesObject *)self, sep, maxsplit);
283
284 exit:
285 return return_value;
286 }
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 {
309 PyObject *return_value = NULL;
310
311 return_value = bytes_join_impl((PyBytesObject *)self, iterable_of_bytes);
312
313 return return_value;
314 }
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 {
339 PyObject *return_value = NULL;
340 PyObject *sub;
341 Py_ssize_t start = 0;
342 Py_ssize_t end = PY_SSIZE_T_MAX;
343
344 if (!_PyArg_CheckPositional("find", nargs, 1, 3)) {
345 goto exit;
346 }
347 sub = args[0];
348 if (nargs < 2) {
349 goto skip_optional;
350 }
351 if (!_PyEval_SliceIndex(args[1], &start)) {
352 goto exit;
353 }
354 if (nargs < 3) {
355 goto skip_optional;
356 }
357 if (!_PyEval_SliceIndex(args[2], &end)) {
358 goto exit;
359 }
360 skip_optional:
361 return_value = bytes_find_impl((PyBytesObject *)self, sub, start, end);
362
363 exit:
364 return return_value;
365 }
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 {
390 PyObject *return_value = NULL;
391 PyObject *sub;
392 Py_ssize_t start = 0;
393 Py_ssize_t end = PY_SSIZE_T_MAX;
394
395 if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
396 goto exit;
397 }
398 sub = args[0];
399 if (nargs < 2) {
400 goto skip_optional;
401 }
402 if (!_PyEval_SliceIndex(args[1], &start)) {
403 goto exit;
404 }
405 if (nargs < 3) {
406 goto skip_optional;
407 }
408 if (!_PyEval_SliceIndex(args[2], &end)) {
409 goto exit;
410 }
411 skip_optional:
412 return_value = bytes_index_impl((PyBytesObject *)self, sub, start, end);
413
414 exit:
415 return return_value;
416 }
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 {
441 PyObject *return_value = NULL;
442 PyObject *sub;
443 Py_ssize_t start = 0;
444 Py_ssize_t end = PY_SSIZE_T_MAX;
445
446 if (!_PyArg_CheckPositional("rfind", nargs, 1, 3)) {
447 goto exit;
448 }
449 sub = args[0];
450 if (nargs < 2) {
451 goto skip_optional;
452 }
453 if (!_PyEval_SliceIndex(args[1], &start)) {
454 goto exit;
455 }
456 if (nargs < 3) {
457 goto skip_optional;
458 }
459 if (!_PyEval_SliceIndex(args[2], &end)) {
460 goto exit;
461 }
462 skip_optional:
463 return_value = bytes_rfind_impl((PyBytesObject *)self, sub, start, end);
464
465 exit:
466 return return_value;
467 }
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 {
492 PyObject *return_value = NULL;
493 PyObject *sub;
494 Py_ssize_t start = 0;
495 Py_ssize_t end = PY_SSIZE_T_MAX;
496
497 if (!_PyArg_CheckPositional("rindex", nargs, 1, 3)) {
498 goto exit;
499 }
500 sub = args[0];
501 if (nargs < 2) {
502 goto skip_optional;
503 }
504 if (!_PyEval_SliceIndex(args[1], &start)) {
505 goto exit;
506 }
507 if (nargs < 3) {
508 goto skip_optional;
509 }
510 if (!_PyEval_SliceIndex(args[2], &end)) {
511 goto exit;
512 }
513 skip_optional:
514 return_value = bytes_rindex_impl((PyBytesObject *)self, sub, start, end);
515
516 exit:
517 return return_value;
518 }
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 {
537 PyObject *return_value = NULL;
538 PyObject *bytes = Py_None;
539
540 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
541 goto exit;
542 }
543 if (nargs < 1) {
544 goto skip_optional;
545 }
546 bytes = args[0];
547 skip_optional:
548 return_value = bytes_strip_impl((PyBytesObject *)self, bytes);
549
550 exit:
551 return return_value;
552 }
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 {
571 PyObject *return_value = NULL;
572 PyObject *bytes = Py_None;
573
574 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
575 goto exit;
576 }
577 if (nargs < 1) {
578 goto skip_optional;
579 }
580 bytes = args[0];
581 skip_optional:
582 return_value = bytes_lstrip_impl((PyBytesObject *)self, bytes);
583
584 exit:
585 return return_value;
586 }
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 {
605 PyObject *return_value = NULL;
606 PyObject *bytes = Py_None;
607
608 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
609 goto exit;
610 }
611 if (nargs < 1) {
612 goto skip_optional;
613 }
614 bytes = args[0];
615 skip_optional:
616 return_value = bytes_rstrip_impl((PyBytesObject *)self, bytes);
617
618 exit:
619 return return_value;
620 }
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 {
643 PyObject *return_value = NULL;
644 PyObject *sub;
645 Py_ssize_t start = 0;
646 Py_ssize_t end = PY_SSIZE_T_MAX;
647
648 if (!_PyArg_CheckPositional("count", nargs, 1, 3)) {
649 goto exit;
650 }
651 sub = args[0];
652 if (nargs < 2) {
653 goto skip_optional;
654 }
655 if (!_PyEval_SliceIndex(args[1], &start)) {
656 goto exit;
657 }
658 if (nargs < 3) {
659 goto skip_optional;
660 }
661 if (!_PyEval_SliceIndex(args[2], &end)) {
662 goto exit;
663 }
664 skip_optional:
665 return_value = bytes_count_impl((PyBytesObject *)self, sub, start, end);
666
667 exit:
668 return return_value;
669 }
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 {
693 PyObject *return_value = NULL;
694 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
695
696 #define NUM_KEYWORDS 1
697 static struct {
698 PyGC_Head _this_is_not_used;
699 PyObject_VAR_HEAD
700 Py_hash_t ob_hash;
701 PyObject *ob_item[NUM_KEYWORDS];
702 } _kwtuple = {
703 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
704 .ob_hash = -1,
705 .ob_item = { &_Py_ID(delete), },
706 };
707 #undef NUM_KEYWORDS
708 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
709
710 #else // !Py_BUILD_CORE
711 # define KWTUPLE NULL
712 #endif // !Py_BUILD_CORE
713
714 static const char * const _keywords[] = {"", "delete", NULL};
715 static _PyArg_Parser _parser = {
716 .keywords = _keywords,
717 .fname = "translate",
718 .kwtuple = KWTUPLE,
719 };
720 #undef KWTUPLE
721 PyObject *argsbuf[2];
722 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
723 PyObject *table;
724 PyObject *deletechars = NULL;
725
726 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
727 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
728 if (!args) {
729 goto exit;
730 }
731 table = args[0];
732 if (!noptargs) {
733 goto skip_optional_pos;
734 }
735 deletechars = args[1];
736 skip_optional_pos:
737 return_value = bytes_translate_impl((PyBytesObject *)self, table, deletechars);
738
739 exit:
740 return return_value;
741 }
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 {
763 PyObject *return_value = NULL;
764 Py_buffer frm = {NULL, NULL};
765 Py_buffer to = {NULL, NULL};
766
767 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
768 goto exit;
769 }
770 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
771 goto exit;
772 }
773 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
774 goto exit;
775 }
776 return_value = bytes_maketrans_impl(&frm, &to);
777
778 exit:
779 /* Cleanup for frm */
780 if (frm.obj) {
781 PyBuffer_Release(&frm);
782 }
783 /* Cleanup for to */
784 if (to.obj) {
785 PyBuffer_Release(&to);
786 }
787
788 return return_value;
789 }
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 {
814 PyObject *return_value = NULL;
815 Py_buffer old = {NULL, NULL};
816 Py_buffer new = {NULL, NULL};
817 Py_ssize_t count = -1;
818
819 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
820 goto exit;
821 }
822 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
823 goto exit;
824 }
825 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
826 goto exit;
827 }
828 if (nargs < 3) {
829 goto skip_optional;
830 }
831 {
832 Py_ssize_t ival = -1;
833 PyObject *iobj = _PyNumber_Index(args[2]);
834 if (iobj != NULL) {
835 ival = PyLong_AsSsize_t(iobj);
836 Py_DECREF(iobj);
837 }
838 if (ival == -1 && PyErr_Occurred()) {
839 goto exit;
840 }
841 count = ival;
842 }
843 skip_optional:
844 return_value = bytes_replace_impl((PyBytesObject *)self, &old, &new, count);
845
846 exit:
847 /* Cleanup for old */
848 if (old.obj) {
849 PyBuffer_Release(&old);
850 }
851 /* Cleanup for new */
852 if (new.obj) {
853 PyBuffer_Release(&new);
854 }
855
856 return return_value;
857 }
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 {
877 PyObject *return_value = NULL;
878 Py_buffer prefix = {NULL, NULL};
879
880 if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
881 goto exit;
882 }
883 return_value = bytes_removeprefix_impl((PyBytesObject *)self, &prefix);
884
885 exit:
886 /* Cleanup for prefix */
887 if (prefix.obj) {
888 PyBuffer_Release(&prefix);
889 }
890
891 return return_value;
892 }
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 {
913 PyObject *return_value = NULL;
914 Py_buffer suffix = {NULL, NULL};
915
916 if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
917 goto exit;
918 }
919 return_value = bytes_removesuffix_impl((PyBytesObject *)self, &suffix);
920
921 exit:
922 /* Cleanup for suffix */
923 if (suffix.obj) {
924 PyBuffer_Release(&suffix);
925 }
926
927 return return_value;
928 }
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 {
953 PyObject *return_value = NULL;
954 PyObject *subobj;
955 Py_ssize_t start = 0;
956 Py_ssize_t end = PY_SSIZE_T_MAX;
957
958 if (!_PyArg_CheckPositional("startswith", nargs, 1, 3)) {
959 goto exit;
960 }
961 subobj = args[0];
962 if (nargs < 2) {
963 goto skip_optional;
964 }
965 if (!_PyEval_SliceIndex(args[1], &start)) {
966 goto exit;
967 }
968 if (nargs < 3) {
969 goto skip_optional;
970 }
971 if (!_PyEval_SliceIndex(args[2], &end)) {
972 goto exit;
973 }
974 skip_optional:
975 return_value = bytes_startswith_impl((PyBytesObject *)self, subobj, start, end);
976
977 exit:
978 return return_value;
979 }
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 {
1004 PyObject *return_value = NULL;
1005 PyObject *subobj;
1006 Py_ssize_t start = 0;
1007 Py_ssize_t end = PY_SSIZE_T_MAX;
1008
1009 if (!_PyArg_CheckPositional("endswith", nargs, 1, 3)) {
1010 goto exit;
1011 }
1012 subobj = args[0];
1013 if (nargs < 2) {
1014 goto skip_optional;
1015 }
1016 if (!_PyEval_SliceIndex(args[1], &start)) {
1017 goto exit;
1018 }
1019 if (nargs < 3) {
1020 goto skip_optional;
1021 }
1022 if (!_PyEval_SliceIndex(args[2], &end)) {
1023 goto exit;
1024 }
1025 skip_optional:
1026 return_value = bytes_endswith_impl((PyBytesObject *)self, subobj, start, end);
1027
1028 exit:
1029 return return_value;
1030 }
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 {
1057 PyObject *return_value = NULL;
1058 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1059
1060 #define NUM_KEYWORDS 2
1061 static struct {
1062 PyGC_Head _this_is_not_used;
1063 PyObject_VAR_HEAD
1064 Py_hash_t ob_hash;
1065 PyObject *ob_item[NUM_KEYWORDS];
1066 } _kwtuple = {
1067 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1068 .ob_hash = -1,
1069 .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
1070 };
1071 #undef NUM_KEYWORDS
1072 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1073
1074 #else // !Py_BUILD_CORE
1075 # define KWTUPLE NULL
1076 #endif // !Py_BUILD_CORE
1077
1078 static const char * const _keywords[] = {"encoding", "errors", NULL};
1079 static _PyArg_Parser _parser = {
1080 .keywords = _keywords,
1081 .fname = "decode",
1082 .kwtuple = KWTUPLE,
1083 };
1084 #undef KWTUPLE
1085 PyObject *argsbuf[2];
1086 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1087 const char *encoding = NULL;
1088 const char *errors = NULL;
1089
1090 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1091 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1092 if (!args) {
1093 goto exit;
1094 }
1095 if (!noptargs) {
1096 goto skip_optional_pos;
1097 }
1098 if (args[0]) {
1099 if (!PyUnicode_Check(args[0])) {
1100 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
1101 goto exit;
1102 }
1103 Py_ssize_t encoding_length;
1104 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
1105 if (encoding == NULL) {
1106 goto exit;
1107 }
1108 if (strlen(encoding) != (size_t)encoding_length) {
1109 PyErr_SetString(PyExc_ValueError, "embedded null character");
1110 goto exit;
1111 }
1112 if (!--noptargs) {
1113 goto skip_optional_pos;
1114 }
1115 }
1116 if (!PyUnicode_Check(args[1])) {
1117 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
1118 goto exit;
1119 }
1120 Py_ssize_t errors_length;
1121 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1122 if (errors == NULL) {
1123 goto exit;
1124 }
1125 if (strlen(errors) != (size_t)errors_length) {
1126 PyErr_SetString(PyExc_ValueError, "embedded null character");
1127 goto exit;
1128 }
1129 skip_optional_pos:
1130 return_value = bytes_decode_impl((PyBytesObject *)self, encoding, errors);
1131
1132 exit:
1133 return return_value;
1134 }
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 {
1154 PyObject *return_value = NULL;
1155 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1156
1157 #define NUM_KEYWORDS 1
1158 static struct {
1159 PyGC_Head _this_is_not_used;
1160 PyObject_VAR_HEAD
1161 Py_hash_t ob_hash;
1162 PyObject *ob_item[NUM_KEYWORDS];
1163 } _kwtuple = {
1164 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1165 .ob_hash = -1,
1166 .ob_item = { &_Py_ID(keepends), },
1167 };
1168 #undef NUM_KEYWORDS
1169 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1170
1171 #else // !Py_BUILD_CORE
1172 # define KWTUPLE NULL
1173 #endif // !Py_BUILD_CORE
1174
1175 static const char * const _keywords[] = {"keepends", NULL};
1176 static _PyArg_Parser _parser = {
1177 .keywords = _keywords,
1178 .fname = "splitlines",
1179 .kwtuple = KWTUPLE,
1180 };
1181 #undef KWTUPLE
1182 PyObject *argsbuf[1];
1183 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1184 int keepends = 0;
1185
1186 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1187 /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1188 if (!args) {
1189 goto exit;
1190 }
1191 if (!noptargs) {
1192 goto skip_optional_pos;
1193 }
1194 keepends = PyObject_IsTrue(args[0]);
1195 if (keepends < 0) {
1196 goto exit;
1197 }
1198 skip_optional_pos:
1199 return_value = bytes_splitlines_impl((PyBytesObject *)self, keepends);
1200
1201 exit:
1202 return return_value;
1203 }
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 {
1223 PyObject *return_value = NULL;
1224
1225 return_value = bytes_fromhex_impl((PyTypeObject *)type, string);
1226
1227 return return_value;
1228 }
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 {
1262 PyObject *return_value = NULL;
1263 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1264
1265 #define NUM_KEYWORDS 2
1266 static struct {
1267 PyGC_Head _this_is_not_used;
1268 PyObject_VAR_HEAD
1269 Py_hash_t ob_hash;
1270 PyObject *ob_item[NUM_KEYWORDS];
1271 } _kwtuple = {
1272 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1273 .ob_hash = -1,
1274 .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
1275 };
1276 #undef NUM_KEYWORDS
1277 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1278
1279 #else // !Py_BUILD_CORE
1280 # define KWTUPLE NULL
1281 #endif // !Py_BUILD_CORE
1282
1283 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
1284 static _PyArg_Parser _parser = {
1285 .keywords = _keywords,
1286 .fname = "hex",
1287 .kwtuple = KWTUPLE,
1288 };
1289 #undef KWTUPLE
1290 PyObject *argsbuf[2];
1291 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1292 PyObject *sep = NULL;
1293 int bytes_per_sep = 1;
1294
1295 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1296 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1297 if (!args) {
1298 goto exit;
1299 }
1300 if (!noptargs) {
1301 goto skip_optional_pos;
1302 }
1303 if (args[0]) {
1304 sep = args[0];
1305 if (!--noptargs) {
1306 goto skip_optional_pos;
1307 }
1308 }
1309 bytes_per_sep = PyLong_AsInt(args[1]);
1310 if (bytes_per_sep == -1 && PyErr_Occurred()) {
1311 goto exit;
1312 }
1313 skip_optional_pos:
1314 return_value = bytes_hex_impl((PyBytesObject *)self, sep, bytes_per_sep);
1315
1316 exit:
1317 return return_value;
1318 }
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 {
1327 PyObject *return_value = NULL;
1328 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1329
1330 #define NUM_KEYWORDS 3
1331 static struct {
1332 PyGC_Head _this_is_not_used;
1333 PyObject_VAR_HEAD
1334 Py_hash_t ob_hash;
1335 PyObject *ob_item[NUM_KEYWORDS];
1336 } _kwtuple = {
1337 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1338 .ob_hash = -1,
1339 .ob_item = { &_Py_ID(source), &_Py_ID(encoding), &_Py_ID(errors), },
1340 };
1341 #undef NUM_KEYWORDS
1342 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1343
1344 #else // !Py_BUILD_CORE
1345 # define KWTUPLE NULL
1346 #endif // !Py_BUILD_CORE
1347
1348 static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
1349 static _PyArg_Parser _parser = {
1350 .keywords = _keywords,
1351 .fname = "bytes",
1352 .kwtuple = KWTUPLE,
1353 };
1354 #undef KWTUPLE
1355 PyObject *argsbuf[3];
1356 PyObject * const *fastargs;
1357 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1358 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
1359 PyObject *x = NULL;
1360 const char *encoding = NULL;
1361 const char *errors = NULL;
1362
1363 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
1364 /*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1365 if (!fastargs) {
1366 goto exit;
1367 }
1368 if (!noptargs) {
1369 goto skip_optional_pos;
1370 }
1371 if (fastargs[0]) {
1372 x = fastargs[0];
1373 if (!--noptargs) {
1374 goto skip_optional_pos;
1375 }
1376 }
1377 if (fastargs[1]) {
1378 if (!PyUnicode_Check(fastargs[1])) {
1379 _PyArg_BadArgument("bytes", "argument 'encoding'", "str", fastargs[1]);
1380 goto exit;
1381 }
1382 Py_ssize_t encoding_length;
1383 encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
1384 if (encoding == NULL) {
1385 goto exit;
1386 }
1387 if (strlen(encoding) != (size_t)encoding_length) {
1388 PyErr_SetString(PyExc_ValueError, "embedded null character");
1389 goto exit;
1390 }
1391 if (!--noptargs) {
1392 goto skip_optional_pos;
1393 }
1394 }
1395 if (!PyUnicode_Check(fastargs[2])) {
1396 _PyArg_BadArgument("bytes", "argument 'errors'", "str", fastargs[2]);
1397 goto exit;
1398 }
1399 Py_ssize_t errors_length;
1400 errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
1401 if (errors == NULL) {
1402 goto exit;
1403 }
1404 if (strlen(errors) != (size_t)errors_length) {
1405 PyErr_SetString(PyExc_ValueError, "embedded null character");
1406 goto exit;
1407 }
1408 skip_optional_pos:
1409 return_value = bytes_new_impl(type, x, encoding, errors);
1410
1411 exit:
1412 return return_value;
1413 }
1414 /*[clinic end generated code: output=08b9507244f73638 input=a9049054013a1b77]*/