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