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
10
11 PyDoc_STRVAR(bytes___bytes____doc__,
12 "__bytes__($self, /)\n"
13 "--\n"
14 "\n"
15 "Convert this value to exact type bytes.");
16
17 #define BYTES___BYTES___METHODDEF \
18 {"__bytes__", (PyCFunction)bytes___bytes__, METH_NOARGS, bytes___bytes____doc__},
19
20 static PyObject *
21 bytes___bytes___impl(PyBytesObject *self);
22
23 static PyObject *
24 bytes___bytes__(PyBytesObject *self, PyObject *Py_UNUSED(ignored))
25 {
26 return bytes___bytes___impl(self);
27 }
28
29 PyDoc_STRVAR(bytes_split__doc__,
30 "split($self, /, sep=None, maxsplit=-1)\n"
31 "--\n"
32 "\n"
33 "Return a list of the sections in the bytes, using sep as the delimiter.\n"
34 "\n"
35 " sep\n"
36 " The delimiter according which to split the bytes.\n"
37 " None (the default value) means split on ASCII whitespace characters\n"
38 " (space, tab, return, newline, formfeed, vertical tab).\n"
39 " maxsplit\n"
40 " Maximum number of splits to do.\n"
41 " -1 (the default value) means no limit.");
42
43 #define BYTES_SPLIT_METHODDEF \
44 {"split", _PyCFunction_CAST(bytes_split), METH_FASTCALL|METH_KEYWORDS, bytes_split__doc__},
45
46 static PyObject *
47 bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit);
48
49 static PyObject *
50 bytes_split(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
51 {
52 PyObject *return_value = NULL;
53 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
54
55 #define NUM_KEYWORDS 2
56 static struct {
57 PyGC_Head _this_is_not_used;
58 PyObject_VAR_HEAD
59 PyObject *ob_item[NUM_KEYWORDS];
60 } _kwtuple = {
61 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
62 .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
63 };
64 #undef NUM_KEYWORDS
65 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
66
67 #else // !Py_BUILD_CORE
68 # define KWTUPLE NULL
69 #endif // !Py_BUILD_CORE
70
71 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
72 static _PyArg_Parser _parser = {
73 .keywords = _keywords,
74 .fname = "split",
75 .kwtuple = KWTUPLE,
76 };
77 #undef KWTUPLE
78 PyObject *argsbuf[2];
79 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
80 PyObject *sep = Py_None;
81 Py_ssize_t maxsplit = -1;
82
83 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
84 if (!args) {
85 goto exit;
86 }
87 if (!noptargs) {
88 goto skip_optional_pos;
89 }
90 if (args[0]) {
91 sep = args[0];
92 if (!--noptargs) {
93 goto skip_optional_pos;
94 }
95 }
96 {
97 Py_ssize_t ival = -1;
98 PyObject *iobj = _PyNumber_Index(args[1]);
99 if (iobj != NULL) {
100 ival = PyLong_AsSsize_t(iobj);
101 Py_DECREF(iobj);
102 }
103 if (ival == -1 && PyErr_Occurred()) {
104 goto exit;
105 }
106 maxsplit = ival;
107 }
108 skip_optional_pos:
109 return_value = bytes_split_impl(self, sep, maxsplit);
110
111 exit:
112 return return_value;
113 }
114
115 PyDoc_STRVAR(bytes_partition__doc__,
116 "partition($self, sep, /)\n"
117 "--\n"
118 "\n"
119 "Partition the bytes into three parts using the given separator.\n"
120 "\n"
121 "This will search for the separator sep in the bytes. If the separator is found,\n"
122 "returns a 3-tuple containing the part before the separator, the separator\n"
123 "itself, and the part after it.\n"
124 "\n"
125 "If the separator is not found, returns a 3-tuple containing the original bytes\n"
126 "object and two empty bytes objects.");
127
128 #define BYTES_PARTITION_METHODDEF \
129 {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__},
130
131 static PyObject *
132 bytes_partition_impl(PyBytesObject *self, Py_buffer *sep);
133
134 static PyObject *
135 bytes_partition(PyBytesObject *self, PyObject *arg)
136 {
137 PyObject *return_value = NULL;
138 Py_buffer sep = {NULL, NULL};
139
140 if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
141 goto exit;
142 }
143 if (!PyBuffer_IsContiguous(&sep, 'C')) {
144 _PyArg_BadArgument("partition", "argument", "contiguous buffer", arg);
145 goto exit;
146 }
147 return_value = bytes_partition_impl(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(PyBytesObject *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 if (!PyBuffer_IsContiguous(&sep, 'C')) {
187 _PyArg_BadArgument("rpartition", "argument", "contiguous buffer", arg);
188 goto exit;
189 }
190 return_value = bytes_rpartition_impl(self, &sep);
191
192 exit:
193 /* Cleanup for sep */
194 if (sep.obj) {
195 PyBuffer_Release(&sep);
196 }
197
198 return return_value;
199 }
200
201 PyDoc_STRVAR(bytes_rsplit__doc__,
202 "rsplit($self, /, sep=None, maxsplit=-1)\n"
203 "--\n"
204 "\n"
205 "Return a list of the sections in the bytes, using sep as the delimiter.\n"
206 "\n"
207 " sep\n"
208 " The delimiter according which to split the bytes.\n"
209 " None (the default value) means split on ASCII whitespace characters\n"
210 " (space, tab, return, newline, formfeed, vertical tab).\n"
211 " maxsplit\n"
212 " Maximum number of splits to do.\n"
213 " -1 (the default value) means no limit.\n"
214 "\n"
215 "Splitting is done starting at the end of the bytes and working to the front.");
216
217 #define BYTES_RSPLIT_METHODDEF \
218 {"rsplit", _PyCFunction_CAST(bytes_rsplit), METH_FASTCALL|METH_KEYWORDS, bytes_rsplit__doc__},
219
220 static PyObject *
221 bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit);
222
223 static PyObject *
224 bytes_rsplit(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
225 {
226 PyObject *return_value = NULL;
227 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
228
229 #define NUM_KEYWORDS 2
230 static struct {
231 PyGC_Head _this_is_not_used;
232 PyObject_VAR_HEAD
233 PyObject *ob_item[NUM_KEYWORDS];
234 } _kwtuple = {
235 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
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, 0, 2, 0, argsbuf);
258 if (!args) {
259 goto exit;
260 }
261 if (!noptargs) {
262 goto skip_optional_pos;
263 }
264 if (args[0]) {
265 sep = args[0];
266 if (!--noptargs) {
267 goto skip_optional_pos;
268 }
269 }
270 {
271 Py_ssize_t ival = -1;
272 PyObject *iobj = _PyNumber_Index(args[1]);
273 if (iobj != NULL) {
274 ival = PyLong_AsSsize_t(iobj);
275 Py_DECREF(iobj);
276 }
277 if (ival == -1 && PyErr_Occurred()) {
278 goto exit;
279 }
280 maxsplit = ival;
281 }
282 skip_optional_pos:
283 return_value = bytes_rsplit_impl(self, sep, maxsplit);
284
285 exit:
286 return return_value;
287 }
288
289 PyDoc_STRVAR(bytes_join__doc__,
290 "join($self, iterable_of_bytes, /)\n"
291 "--\n"
292 "\n"
293 "Concatenate any number of bytes objects.\n"
294 "\n"
295 "The bytes whose method is called is inserted in between each pair.\n"
296 "\n"
297 "The result is returned as a new bytes object.\n"
298 "\n"
299 "Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'.");
300
301 #define BYTES_JOIN_METHODDEF \
302 {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__},
303
304 PyDoc_STRVAR(bytes_strip__doc__,
305 "strip($self, bytes=None, /)\n"
306 "--\n"
307 "\n"
308 "Strip leading and trailing bytes contained in the argument.\n"
309 "\n"
310 "If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
311
312 #define BYTES_STRIP_METHODDEF \
313 {"strip", _PyCFunction_CAST(bytes_strip), METH_FASTCALL, bytes_strip__doc__},
314
315 static PyObject *
316 bytes_strip_impl(PyBytesObject *self, PyObject *bytes);
317
318 static PyObject *
319 bytes_strip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
320 {
321 PyObject *return_value = NULL;
322 PyObject *bytes = Py_None;
323
324 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
325 goto exit;
326 }
327 if (nargs < 1) {
328 goto skip_optional;
329 }
330 bytes = args[0];
331 skip_optional:
332 return_value = bytes_strip_impl(self, bytes);
333
334 exit:
335 return return_value;
336 }
337
338 PyDoc_STRVAR(bytes_lstrip__doc__,
339 "lstrip($self, bytes=None, /)\n"
340 "--\n"
341 "\n"
342 "Strip leading bytes contained in the argument.\n"
343 "\n"
344 "If the argument is omitted or None, strip leading ASCII whitespace.");
345
346 #define BYTES_LSTRIP_METHODDEF \
347 {"lstrip", _PyCFunction_CAST(bytes_lstrip), METH_FASTCALL, bytes_lstrip__doc__},
348
349 static PyObject *
350 bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes);
351
352 static PyObject *
353 bytes_lstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
354 {
355 PyObject *return_value = NULL;
356 PyObject *bytes = Py_None;
357
358 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
359 goto exit;
360 }
361 if (nargs < 1) {
362 goto skip_optional;
363 }
364 bytes = args[0];
365 skip_optional:
366 return_value = bytes_lstrip_impl(self, bytes);
367
368 exit:
369 return return_value;
370 }
371
372 PyDoc_STRVAR(bytes_rstrip__doc__,
373 "rstrip($self, bytes=None, /)\n"
374 "--\n"
375 "\n"
376 "Strip trailing bytes contained in the argument.\n"
377 "\n"
378 "If the argument is omitted or None, strip trailing ASCII whitespace.");
379
380 #define BYTES_RSTRIP_METHODDEF \
381 {"rstrip", _PyCFunction_CAST(bytes_rstrip), METH_FASTCALL, bytes_rstrip__doc__},
382
383 static PyObject *
384 bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes);
385
386 static PyObject *
387 bytes_rstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
388 {
389 PyObject *return_value = NULL;
390 PyObject *bytes = Py_None;
391
392 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
393 goto exit;
394 }
395 if (nargs < 1) {
396 goto skip_optional;
397 }
398 bytes = args[0];
399 skip_optional:
400 return_value = bytes_rstrip_impl(self, bytes);
401
402 exit:
403 return return_value;
404 }
405
406 PyDoc_STRVAR(bytes_translate__doc__,
407 "translate($self, table, /, delete=b\'\')\n"
408 "--\n"
409 "\n"
410 "Return a copy with each character mapped by the given translation table.\n"
411 "\n"
412 " table\n"
413 " Translation table, which must be a bytes object of length 256.\n"
414 "\n"
415 "All characters occurring in the optional argument delete are removed.\n"
416 "The remaining characters are mapped through the given translation table.");
417
418 #define BYTES_TRANSLATE_METHODDEF \
419 {"translate", _PyCFunction_CAST(bytes_translate), METH_FASTCALL|METH_KEYWORDS, bytes_translate__doc__},
420
421 static PyObject *
422 bytes_translate_impl(PyBytesObject *self, PyObject *table,
423 PyObject *deletechars);
424
425 static PyObject *
426 bytes_translate(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
427 {
428 PyObject *return_value = NULL;
429 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
430
431 #define NUM_KEYWORDS 1
432 static struct {
433 PyGC_Head _this_is_not_used;
434 PyObject_VAR_HEAD
435 PyObject *ob_item[NUM_KEYWORDS];
436 } _kwtuple = {
437 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
438 .ob_item = { &_Py_ID(delete), },
439 };
440 #undef NUM_KEYWORDS
441 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
442
443 #else // !Py_BUILD_CORE
444 # define KWTUPLE NULL
445 #endif // !Py_BUILD_CORE
446
447 static const char * const _keywords[] = {"", "delete", NULL};
448 static _PyArg_Parser _parser = {
449 .keywords = _keywords,
450 .fname = "translate",
451 .kwtuple = KWTUPLE,
452 };
453 #undef KWTUPLE
454 PyObject *argsbuf[2];
455 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
456 PyObject *table;
457 PyObject *deletechars = NULL;
458
459 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
460 if (!args) {
461 goto exit;
462 }
463 table = args[0];
464 if (!noptargs) {
465 goto skip_optional_pos;
466 }
467 deletechars = args[1];
468 skip_optional_pos:
469 return_value = bytes_translate_impl(self, table, deletechars);
470
471 exit:
472 return return_value;
473 }
474
475 PyDoc_STRVAR(bytes_maketrans__doc__,
476 "maketrans(frm, to, /)\n"
477 "--\n"
478 "\n"
479 "Return a translation table useable for the bytes or bytearray translate method.\n"
480 "\n"
481 "The returned table will be one where each byte in frm is mapped to the byte at\n"
482 "the same position in to.\n"
483 "\n"
484 "The bytes objects frm and to must be of the same length.");
485
486 #define BYTES_MAKETRANS_METHODDEF \
487 {"maketrans", _PyCFunction_CAST(bytes_maketrans), METH_FASTCALL|METH_STATIC, bytes_maketrans__doc__},
488
489 static PyObject *
490 bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to);
491
492 static PyObject *
493 bytes_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
494 {
495 PyObject *return_value = NULL;
496 Py_buffer frm = {NULL, NULL};
497 Py_buffer to = {NULL, NULL};
498
499 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
500 goto exit;
501 }
502 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
503 goto exit;
504 }
505 if (!PyBuffer_IsContiguous(&frm, 'C')) {
506 _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]);
507 goto exit;
508 }
509 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
510 goto exit;
511 }
512 if (!PyBuffer_IsContiguous(&to, 'C')) {
513 _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]);
514 goto exit;
515 }
516 return_value = bytes_maketrans_impl(&frm, &to);
517
518 exit:
519 /* Cleanup for frm */
520 if (frm.obj) {
521 PyBuffer_Release(&frm);
522 }
523 /* Cleanup for to */
524 if (to.obj) {
525 PyBuffer_Release(&to);
526 }
527
528 return return_value;
529 }
530
531 PyDoc_STRVAR(bytes_replace__doc__,
532 "replace($self, old, new, count=-1, /)\n"
533 "--\n"
534 "\n"
535 "Return a copy with all occurrences of substring old replaced by new.\n"
536 "\n"
537 " count\n"
538 " Maximum number of occurrences to replace.\n"
539 " -1 (the default value) means replace all occurrences.\n"
540 "\n"
541 "If the optional argument count is given, only the first count occurrences are\n"
542 "replaced.");
543
544 #define BYTES_REPLACE_METHODDEF \
545 {"replace", _PyCFunction_CAST(bytes_replace), METH_FASTCALL, bytes_replace__doc__},
546
547 static PyObject *
548 bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
549 Py_ssize_t count);
550
551 static PyObject *
552 bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
553 {
554 PyObject *return_value = NULL;
555 Py_buffer old = {NULL, NULL};
556 Py_buffer new = {NULL, NULL};
557 Py_ssize_t count = -1;
558
559 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
560 goto exit;
561 }
562 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
563 goto exit;
564 }
565 if (!PyBuffer_IsContiguous(&old, 'C')) {
566 _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]);
567 goto exit;
568 }
569 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
570 goto exit;
571 }
572 if (!PyBuffer_IsContiguous(&new, 'C')) {
573 _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]);
574 goto exit;
575 }
576 if (nargs < 3) {
577 goto skip_optional;
578 }
579 {
580 Py_ssize_t ival = -1;
581 PyObject *iobj = _PyNumber_Index(args[2]);
582 if (iobj != NULL) {
583 ival = PyLong_AsSsize_t(iobj);
584 Py_DECREF(iobj);
585 }
586 if (ival == -1 && PyErr_Occurred()) {
587 goto exit;
588 }
589 count = ival;
590 }
591 skip_optional:
592 return_value = bytes_replace_impl(self, &old, &new, count);
593
594 exit:
595 /* Cleanup for old */
596 if (old.obj) {
597 PyBuffer_Release(&old);
598 }
599 /* Cleanup for new */
600 if (new.obj) {
601 PyBuffer_Release(&new);
602 }
603
604 return return_value;
605 }
606
607 PyDoc_STRVAR(bytes_removeprefix__doc__,
608 "removeprefix($self, prefix, /)\n"
609 "--\n"
610 "\n"
611 "Return a bytes object with the given prefix string removed if present.\n"
612 "\n"
613 "If the bytes starts with the prefix string, return bytes[len(prefix):].\n"
614 "Otherwise, return a copy of the original bytes.");
615
616 #define BYTES_REMOVEPREFIX_METHODDEF \
617 {"removeprefix", (PyCFunction)bytes_removeprefix, METH_O, bytes_removeprefix__doc__},
618
619 static PyObject *
620 bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix);
621
622 static PyObject *
623 bytes_removeprefix(PyBytesObject *self, PyObject *arg)
624 {
625 PyObject *return_value = NULL;
626 Py_buffer prefix = {NULL, NULL};
627
628 if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
629 goto exit;
630 }
631 if (!PyBuffer_IsContiguous(&prefix, 'C')) {
632 _PyArg_BadArgument("removeprefix", "argument", "contiguous buffer", arg);
633 goto exit;
634 }
635 return_value = bytes_removeprefix_impl(self, &prefix);
636
637 exit:
638 /* Cleanup for prefix */
639 if (prefix.obj) {
640 PyBuffer_Release(&prefix);
641 }
642
643 return return_value;
644 }
645
646 PyDoc_STRVAR(bytes_removesuffix__doc__,
647 "removesuffix($self, suffix, /)\n"
648 "--\n"
649 "\n"
650 "Return a bytes object with the given suffix string removed if present.\n"
651 "\n"
652 "If the bytes ends with the suffix string and that suffix is not empty,\n"
653 "return bytes[:-len(prefix)]. Otherwise, return a copy of the original\n"
654 "bytes.");
655
656 #define BYTES_REMOVESUFFIX_METHODDEF \
657 {"removesuffix", (PyCFunction)bytes_removesuffix, METH_O, bytes_removesuffix__doc__},
658
659 static PyObject *
660 bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix);
661
662 static PyObject *
663 bytes_removesuffix(PyBytesObject *self, PyObject *arg)
664 {
665 PyObject *return_value = NULL;
666 Py_buffer suffix = {NULL, NULL};
667
668 if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
669 goto exit;
670 }
671 if (!PyBuffer_IsContiguous(&suffix, 'C')) {
672 _PyArg_BadArgument("removesuffix", "argument", "contiguous buffer", arg);
673 goto exit;
674 }
675 return_value = bytes_removesuffix_impl(self, &suffix);
676
677 exit:
678 /* Cleanup for suffix */
679 if (suffix.obj) {
680 PyBuffer_Release(&suffix);
681 }
682
683 return return_value;
684 }
685
686 PyDoc_STRVAR(bytes_decode__doc__,
687 "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
688 "--\n"
689 "\n"
690 "Decode the bytes using the codec registered for encoding.\n"
691 "\n"
692 " encoding\n"
693 " The encoding with which to decode the bytes.\n"
694 " errors\n"
695 " The error handling scheme to use for the handling of decoding errors.\n"
696 " The default is \'strict\' meaning that decoding errors raise a\n"
697 " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
698 " as well as any other name registered with codecs.register_error that\n"
699 " can handle UnicodeDecodeErrors.");
700
701 #define BYTES_DECODE_METHODDEF \
702 {"decode", _PyCFunction_CAST(bytes_decode), METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__},
703
704 static PyObject *
705 bytes_decode_impl(PyBytesObject *self, const char *encoding,
706 const char *errors);
707
708 static PyObject *
709 bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
710 {
711 PyObject *return_value = NULL;
712 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
713
714 #define NUM_KEYWORDS 2
715 static struct {
716 PyGC_Head _this_is_not_used;
717 PyObject_VAR_HEAD
718 PyObject *ob_item[NUM_KEYWORDS];
719 } _kwtuple = {
720 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
721 .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
722 };
723 #undef NUM_KEYWORDS
724 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
725
726 #else // !Py_BUILD_CORE
727 # define KWTUPLE NULL
728 #endif // !Py_BUILD_CORE
729
730 static const char * const _keywords[] = {"encoding", "errors", NULL};
731 static _PyArg_Parser _parser = {
732 .keywords = _keywords,
733 .fname = "decode",
734 .kwtuple = KWTUPLE,
735 };
736 #undef KWTUPLE
737 PyObject *argsbuf[2];
738 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
739 const char *encoding = NULL;
740 const char *errors = NULL;
741
742 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
743 if (!args) {
744 goto exit;
745 }
746 if (!noptargs) {
747 goto skip_optional_pos;
748 }
749 if (args[0]) {
750 if (!PyUnicode_Check(args[0])) {
751 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
752 goto exit;
753 }
754 Py_ssize_t encoding_length;
755 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
756 if (encoding == NULL) {
757 goto exit;
758 }
759 if (strlen(encoding) != (size_t)encoding_length) {
760 PyErr_SetString(PyExc_ValueError, "embedded null character");
761 goto exit;
762 }
763 if (!--noptargs) {
764 goto skip_optional_pos;
765 }
766 }
767 if (!PyUnicode_Check(args[1])) {
768 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
769 goto exit;
770 }
771 Py_ssize_t errors_length;
772 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
773 if (errors == NULL) {
774 goto exit;
775 }
776 if (strlen(errors) != (size_t)errors_length) {
777 PyErr_SetString(PyExc_ValueError, "embedded null character");
778 goto exit;
779 }
780 skip_optional_pos:
781 return_value = bytes_decode_impl(self, encoding, errors);
782
783 exit:
784 return return_value;
785 }
786
787 PyDoc_STRVAR(bytes_splitlines__doc__,
788 "splitlines($self, /, keepends=False)\n"
789 "--\n"
790 "\n"
791 "Return a list of the lines in the bytes, breaking at line boundaries.\n"
792 "\n"
793 "Line breaks are not included in the resulting list unless keepends is given and\n"
794 "true.");
795
796 #define BYTES_SPLITLINES_METHODDEF \
797 {"splitlines", _PyCFunction_CAST(bytes_splitlines), METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__},
798
799 static PyObject *
800 bytes_splitlines_impl(PyBytesObject *self, int keepends);
801
802 static PyObject *
803 bytes_splitlines(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
804 {
805 PyObject *return_value = NULL;
806 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
807
808 #define NUM_KEYWORDS 1
809 static struct {
810 PyGC_Head _this_is_not_used;
811 PyObject_VAR_HEAD
812 PyObject *ob_item[NUM_KEYWORDS];
813 } _kwtuple = {
814 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
815 .ob_item = { &_Py_ID(keepends), },
816 };
817 #undef NUM_KEYWORDS
818 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
819
820 #else // !Py_BUILD_CORE
821 # define KWTUPLE NULL
822 #endif // !Py_BUILD_CORE
823
824 static const char * const _keywords[] = {"keepends", NULL};
825 static _PyArg_Parser _parser = {
826 .keywords = _keywords,
827 .fname = "splitlines",
828 .kwtuple = KWTUPLE,
829 };
830 #undef KWTUPLE
831 PyObject *argsbuf[1];
832 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
833 int keepends = 0;
834
835 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
836 if (!args) {
837 goto exit;
838 }
839 if (!noptargs) {
840 goto skip_optional_pos;
841 }
842 keepends = PyObject_IsTrue(args[0]);
843 if (keepends < 0) {
844 goto exit;
845 }
846 skip_optional_pos:
847 return_value = bytes_splitlines_impl(self, keepends);
848
849 exit:
850 return return_value;
851 }
852
853 PyDoc_STRVAR(bytes_fromhex__doc__,
854 "fromhex($type, string, /)\n"
855 "--\n"
856 "\n"
857 "Create a bytes object from a string of hexadecimal numbers.\n"
858 "\n"
859 "Spaces between two numbers are accepted.\n"
860 "Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'.");
861
862 #define BYTES_FROMHEX_METHODDEF \
863 {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__},
864
865 static PyObject *
866 bytes_fromhex_impl(PyTypeObject *type, PyObject *string);
867
868 static PyObject *
869 bytes_fromhex(PyTypeObject *type, PyObject *arg)
870 {
871 PyObject *return_value = NULL;
872 PyObject *string;
873
874 if (!PyUnicode_Check(arg)) {
875 _PyArg_BadArgument("fromhex", "argument", "str", arg);
876 goto exit;
877 }
878 if (PyUnicode_READY(arg) == -1) {
879 goto exit;
880 }
881 string = arg;
882 return_value = bytes_fromhex_impl(type, string);
883
884 exit:
885 return return_value;
886 }
887
888 PyDoc_STRVAR(bytes_hex__doc__,
889 "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
890 "--\n"
891 "\n"
892 "Create a string of hexadecimal numbers from a bytes object.\n"
893 "\n"
894 " sep\n"
895 " An optional single character or byte to separate hex bytes.\n"
896 " bytes_per_sep\n"
897 " How many bytes between separators. Positive values count from the\n"
898 " right, negative values count from the left.\n"
899 "\n"
900 "Example:\n"
901 ">>> value = b\'\\xb9\\x01\\xef\'\n"
902 ">>> value.hex()\n"
903 "\'b901ef\'\n"
904 ">>> value.hex(\':\')\n"
905 "\'b9:01:ef\'\n"
906 ">>> value.hex(\':\', 2)\n"
907 "\'b9:01ef\'\n"
908 ">>> value.hex(\':\', -2)\n"
909 "\'b901:ef\'");
910
911 #define BYTES_HEX_METHODDEF \
912 {"hex", _PyCFunction_CAST(bytes_hex), METH_FASTCALL|METH_KEYWORDS, bytes_hex__doc__},
913
914 static PyObject *
915 bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep);
916
917 static PyObject *
918 bytes_hex(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
919 {
920 PyObject *return_value = NULL;
921 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
922
923 #define NUM_KEYWORDS 2
924 static struct {
925 PyGC_Head _this_is_not_used;
926 PyObject_VAR_HEAD
927 PyObject *ob_item[NUM_KEYWORDS];
928 } _kwtuple = {
929 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
930 .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
931 };
932 #undef NUM_KEYWORDS
933 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
934
935 #else // !Py_BUILD_CORE
936 # define KWTUPLE NULL
937 #endif // !Py_BUILD_CORE
938
939 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
940 static _PyArg_Parser _parser = {
941 .keywords = _keywords,
942 .fname = "hex",
943 .kwtuple = KWTUPLE,
944 };
945 #undef KWTUPLE
946 PyObject *argsbuf[2];
947 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
948 PyObject *sep = NULL;
949 int bytes_per_sep = 1;
950
951 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
952 if (!args) {
953 goto exit;
954 }
955 if (!noptargs) {
956 goto skip_optional_pos;
957 }
958 if (args[0]) {
959 sep = args[0];
960 if (!--noptargs) {
961 goto skip_optional_pos;
962 }
963 }
964 bytes_per_sep = _PyLong_AsInt(args[1]);
965 if (bytes_per_sep == -1 && PyErr_Occurred()) {
966 goto exit;
967 }
968 skip_optional_pos:
969 return_value = bytes_hex_impl(self, sep, bytes_per_sep);
970
971 exit:
972 return return_value;
973 }
974
975 static PyObject *
976 bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
977 const char *errors);
978
979 static PyObject *
980 bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
981 {
982 PyObject *return_value = NULL;
983 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
984
985 #define NUM_KEYWORDS 3
986 static struct {
987 PyGC_Head _this_is_not_used;
988 PyObject_VAR_HEAD
989 PyObject *ob_item[NUM_KEYWORDS];
990 } _kwtuple = {
991 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
992 .ob_item = { &_Py_ID(source), &_Py_ID(encoding), &_Py_ID(errors), },
993 };
994 #undef NUM_KEYWORDS
995 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
996
997 #else // !Py_BUILD_CORE
998 # define KWTUPLE NULL
999 #endif // !Py_BUILD_CORE
1000
1001 static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
1002 static _PyArg_Parser _parser = {
1003 .keywords = _keywords,
1004 .fname = "bytes",
1005 .kwtuple = KWTUPLE,
1006 };
1007 #undef KWTUPLE
1008 PyObject *argsbuf[3];
1009 PyObject * const *fastargs;
1010 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
1011 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
1012 PyObject *x = NULL;
1013 const char *encoding = NULL;
1014 const char *errors = NULL;
1015
1016 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
1017 if (!fastargs) {
1018 goto exit;
1019 }
1020 if (!noptargs) {
1021 goto skip_optional_pos;
1022 }
1023 if (fastargs[0]) {
1024 x = fastargs[0];
1025 if (!--noptargs) {
1026 goto skip_optional_pos;
1027 }
1028 }
1029 if (fastargs[1]) {
1030 if (!PyUnicode_Check(fastargs[1])) {
1031 _PyArg_BadArgument("bytes", "argument 'encoding'", "str", fastargs[1]);
1032 goto exit;
1033 }
1034 Py_ssize_t encoding_length;
1035 encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
1036 if (encoding == NULL) {
1037 goto exit;
1038 }
1039 if (strlen(encoding) != (size_t)encoding_length) {
1040 PyErr_SetString(PyExc_ValueError, "embedded null character");
1041 goto exit;
1042 }
1043 if (!--noptargs) {
1044 goto skip_optional_pos;
1045 }
1046 }
1047 if (!PyUnicode_Check(fastargs[2])) {
1048 _PyArg_BadArgument("bytes", "argument 'errors'", "str", fastargs[2]);
1049 goto exit;
1050 }
1051 Py_ssize_t errors_length;
1052 errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
1053 if (errors == NULL) {
1054 goto exit;
1055 }
1056 if (strlen(errors) != (size_t)errors_length) {
1057 PyErr_SetString(PyExc_ValueError, "embedded null character");
1058 goto exit;
1059 }
1060 skip_optional_pos:
1061 return_value = bytes_new_impl(type, x, encoding, errors);
1062
1063 exit:
1064 return return_value;
1065 }
1066 /*[clinic end generated code: output=31a9e4af85562612 input=a9049054013a1b77]*/