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