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