Line data Source code
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 *
18 115419 : bytes___bytes__(PyBytesObject *self, PyObject *Py_UNUSED(ignored))
19 : {
20 115419 : 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 *
44 7992 : bytes_split(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
45 : {
46 7992 : 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 7992 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
51 7992 : PyObject *sep = Py_None;
52 7992 : Py_ssize_t maxsplit = -1;
53 :
54 7992 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
55 7992 : if (!args) {
56 1 : goto exit;
57 : }
58 7991 : if (!noptargs) {
59 96 : goto skip_optional_pos;
60 : }
61 7895 : if (args[0]) {
62 7894 : sep = args[0];
63 7894 : if (!--noptargs) {
64 3226 : goto skip_optional_pos;
65 : }
66 : }
67 : {
68 4669 : Py_ssize_t ival = -1;
69 4669 : PyObject *iobj = _PyNumber_Index(args[1]);
70 4669 : if (iobj != NULL) {
71 4669 : ival = PyLong_AsSsize_t(iobj);
72 4669 : Py_DECREF(iobj);
73 : }
74 4669 : if (ival == -1 && PyErr_Occurred()) {
75 0 : goto exit;
76 : }
77 4669 : maxsplit = ival;
78 : }
79 7991 : skip_optional_pos:
80 7991 : return_value = bytes_split_impl(self, sep, maxsplit);
81 :
82 7992 : exit:
83 7992 : 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 *
106 891 : bytes_partition(PyBytesObject *self, PyObject *arg)
107 : {
108 891 : PyObject *return_value = NULL;
109 891 : Py_buffer sep = {NULL, NULL};
110 :
111 891 : if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
112 2 : goto exit;
113 : }
114 889 : if (!PyBuffer_IsContiguous(&sep, 'C')) {
115 0 : _PyArg_BadArgument("partition", "argument", "contiguous buffer", arg);
116 0 : goto exit;
117 : }
118 889 : return_value = bytes_partition_impl(self, &sep);
119 :
120 891 : exit:
121 : /* Cleanup for sep */
122 891 : if (sep.obj) {
123 889 : PyBuffer_Release(&sep);
124 : }
125 :
126 891 : 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 *
149 343 : bytes_rpartition(PyBytesObject *self, PyObject *arg)
150 : {
151 343 : PyObject *return_value = NULL;
152 343 : Py_buffer sep = {NULL, NULL};
153 :
154 343 : if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
155 2 : goto exit;
156 : }
157 341 : if (!PyBuffer_IsContiguous(&sep, 'C')) {
158 0 : _PyArg_BadArgument("rpartition", "argument", "contiguous buffer", arg);
159 0 : goto exit;
160 : }
161 341 : return_value = bytes_rpartition_impl(self, &sep);
162 :
163 343 : exit:
164 : /* Cleanup for sep */
165 343 : if (sep.obj) {
166 341 : PyBuffer_Release(&sep);
167 : }
168 :
169 343 : 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 *
195 88 : bytes_rsplit(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
196 : {
197 88 : 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 88 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
202 88 : PyObject *sep = Py_None;
203 88 : Py_ssize_t maxsplit = -1;
204 :
205 88 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
206 88 : if (!args) {
207 1 : goto exit;
208 : }
209 87 : if (!noptargs) {
210 12 : goto skip_optional_pos;
211 : }
212 75 : if (args[0]) {
213 74 : sep = args[0];
214 74 : if (!--noptargs) {
215 30 : goto skip_optional_pos;
216 : }
217 : }
218 : {
219 45 : Py_ssize_t ival = -1;
220 45 : PyObject *iobj = _PyNumber_Index(args[1]);
221 45 : if (iobj != NULL) {
222 45 : ival = PyLong_AsSsize_t(iobj);
223 45 : Py_DECREF(iobj);
224 : }
225 45 : if (ival == -1 && PyErr_Occurred()) {
226 0 : goto exit;
227 : }
228 45 : maxsplit = ival;
229 : }
230 87 : skip_optional_pos:
231 87 : return_value = bytes_rsplit_impl(self, sep, maxsplit);
232 :
233 88 : exit:
234 88 : 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 *
267 14478 : bytes_strip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
268 : {
269 14478 : PyObject *return_value = NULL;
270 14478 : PyObject *bytes = Py_None;
271 :
272 14478 : if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
273 1 : goto exit;
274 : }
275 14477 : if (nargs < 1) {
276 13857 : goto skip_optional;
277 : }
278 620 : bytes = args[0];
279 14477 : skip_optional:
280 14477 : return_value = bytes_strip_impl(self, bytes);
281 :
282 14478 : exit:
283 14478 : 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 *
301 140 : bytes_lstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
302 : {
303 140 : PyObject *return_value = NULL;
304 140 : PyObject *bytes = Py_None;
305 :
306 140 : if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
307 1 : goto exit;
308 : }
309 139 : if (nargs < 1) {
310 7 : goto skip_optional;
311 : }
312 132 : bytes = args[0];
313 139 : skip_optional:
314 139 : return_value = bytes_lstrip_impl(self, bytes);
315 :
316 140 : exit:
317 140 : 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 *
335 5872 : bytes_rstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
336 : {
337 5872 : PyObject *return_value = NULL;
338 5872 : PyObject *bytes = Py_None;
339 :
340 5872 : if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
341 1 : goto exit;
342 : }
343 5871 : if (nargs < 1) {
344 211 : goto skip_optional;
345 : }
346 5660 : bytes = args[0];
347 5871 : skip_optional:
348 5871 : return_value = bytes_rstrip_impl(self, bytes);
349 :
350 5872 : exit:
351 5872 : 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 *
374 180 : bytes_translate(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
375 : {
376 180 : 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 180 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
381 : PyObject *table;
382 180 : PyObject *deletechars = NULL;
383 :
384 180 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
385 180 : if (!args) {
386 1 : goto exit;
387 : }
388 179 : table = args[0];
389 179 : if (!noptargs) {
390 171 : goto skip_optional_pos;
391 : }
392 8 : deletechars = args[1];
393 179 : skip_optional_pos:
394 179 : return_value = bytes_translate_impl(self, table, deletechars);
395 :
396 180 : exit:
397 180 : 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 *
418 980 : bytes_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
419 : {
420 980 : PyObject *return_value = NULL;
421 980 : Py_buffer frm = {NULL, NULL};
422 980 : Py_buffer to = {NULL, NULL};
423 :
424 980 : if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
425 0 : goto exit;
426 : }
427 980 : if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
428 1 : goto exit;
429 : }
430 979 : if (!PyBuffer_IsContiguous(&frm, 'C')) {
431 0 : _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]);
432 0 : goto exit;
433 : }
434 979 : if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
435 1 : goto exit;
436 : }
437 978 : if (!PyBuffer_IsContiguous(&to, 'C')) {
438 0 : _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]);
439 0 : goto exit;
440 : }
441 978 : return_value = bytes_maketrans_impl(&frm, &to);
442 :
443 980 : exit:
444 : /* Cleanup for frm */
445 980 : if (frm.obj) {
446 979 : PyBuffer_Release(&frm);
447 : }
448 : /* Cleanup for to */
449 980 : if (to.obj) {
450 978 : PyBuffer_Release(&to);
451 : }
452 :
453 980 : 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 *
477 108223 : bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
478 : {
479 108223 : PyObject *return_value = NULL;
480 108223 : Py_buffer old = {NULL, NULL};
481 108223 : Py_buffer new = {NULL, NULL};
482 108223 : Py_ssize_t count = -1;
483 :
484 108223 : if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
485 2 : goto exit;
486 : }
487 108221 : if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
488 5 : goto exit;
489 : }
490 108216 : if (!PyBuffer_IsContiguous(&old, 'C')) {
491 0 : _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]);
492 0 : goto exit;
493 : }
494 108216 : if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
495 1 : goto exit;
496 : }
497 108215 : if (!PyBuffer_IsContiguous(&new, 'C')) {
498 0 : _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]);
499 0 : goto exit;
500 : }
501 108215 : if (nargs < 3) {
502 108134 : goto skip_optional;
503 : }
504 : {
505 81 : Py_ssize_t ival = -1;
506 81 : PyObject *iobj = _PyNumber_Index(args[2]);
507 81 : if (iobj != NULL) {
508 81 : ival = PyLong_AsSsize_t(iobj);
509 81 : Py_DECREF(iobj);
510 : }
511 81 : if (ival == -1 && PyErr_Occurred()) {
512 0 : goto exit;
513 : }
514 81 : count = ival;
515 : }
516 108215 : skip_optional:
517 108215 : return_value = bytes_replace_impl(self, &old, &new, count);
518 :
519 108223 : exit:
520 : /* Cleanup for old */
521 108223 : if (old.obj) {
522 108216 : PyBuffer_Release(&old);
523 : }
524 : /* Cleanup for new */
525 108223 : if (new.obj) {
526 108215 : PyBuffer_Release(&new);
527 : }
528 :
529 108223 : 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 *
548 29 : bytes_removeprefix(PyBytesObject *self, PyObject *arg)
549 : {
550 29 : PyObject *return_value = NULL;
551 29 : Py_buffer prefix = {NULL, NULL};
552 :
553 29 : if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
554 2 : goto exit;
555 : }
556 27 : if (!PyBuffer_IsContiguous(&prefix, 'C')) {
557 0 : _PyArg_BadArgument("removeprefix", "argument", "contiguous buffer", arg);
558 0 : goto exit;
559 : }
560 27 : return_value = bytes_removeprefix_impl(self, &prefix);
561 :
562 29 : exit:
563 : /* Cleanup for prefix */
564 29 : if (prefix.obj) {
565 27 : PyBuffer_Release(&prefix);
566 : }
567 :
568 29 : 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 *
588 17 : bytes_removesuffix(PyBytesObject *self, PyObject *arg)
589 : {
590 17 : PyObject *return_value = NULL;
591 17 : Py_buffer suffix = {NULL, NULL};
592 :
593 17 : if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
594 2 : goto exit;
595 : }
596 15 : if (!PyBuffer_IsContiguous(&suffix, 'C')) {
597 0 : _PyArg_BadArgument("removesuffix", "argument", "contiguous buffer", arg);
598 0 : goto exit;
599 : }
600 15 : return_value = bytes_removesuffix_impl(self, &suffix);
601 :
602 17 : exit:
603 : /* Cleanup for suffix */
604 17 : if (suffix.obj) {
605 15 : PyBuffer_Release(&suffix);
606 : }
607 :
608 17 : 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 *
634 7345400 : bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
635 : {
636 7345400 : 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 7345400 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
641 7345400 : const char *encoding = NULL;
642 7345400 : const char *errors = NULL;
643 :
644 7345400 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
645 7345400 : if (!args) {
646 0 : goto exit;
647 : }
648 7345400 : if (!noptargs) {
649 5070 : goto skip_optional_pos;
650 : }
651 7340330 : if (args[0]) {
652 7340330 : if (!PyUnicode_Check(args[0])) {
653 0 : _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
654 1 : goto exit;
655 : }
656 : Py_ssize_t encoding_length;
657 7340330 : encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
658 7340330 : if (encoding == NULL) {
659 1 : goto exit;
660 : }
661 7340330 : if (strlen(encoding) != (size_t)encoding_length) {
662 0 : PyErr_SetString(PyExc_ValueError, "embedded null character");
663 0 : goto exit;
664 : }
665 7340330 : if (!--noptargs) {
666 5962240 : goto skip_optional_pos;
667 : }
668 : }
669 1378100 : if (!PyUnicode_Check(args[1])) {
670 0 : _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
671 0 : goto exit;
672 : }
673 : Py_ssize_t errors_length;
674 1378100 : errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
675 1378100 : if (errors == NULL) {
676 0 : goto exit;
677 : }
678 1378100 : if (strlen(errors) != (size_t)errors_length) {
679 0 : PyErr_SetString(PyExc_ValueError, "embedded null character");
680 0 : goto exit;
681 : }
682 1378100 : skip_optional_pos:
683 7345400 : return_value = bytes_decode_impl(self, encoding, errors);
684 :
685 7345400 : exit:
686 7345400 : 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 *
705 1869 : bytes_splitlines(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
706 : {
707 1869 : 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 1869 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
712 1869 : int keepends = 0;
713 :
714 1869 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
715 1869 : if (!args) {
716 1 : goto exit;
717 : }
718 1868 : if (!noptargs) {
719 176 : goto skip_optional_pos;
720 : }
721 1692 : keepends = _PyLong_AsInt(args[0]);
722 1692 : if (keepends == -1 && PyErr_Occurred()) {
723 0 : goto exit;
724 : }
725 1692 : skip_optional_pos:
726 1868 : return_value = bytes_splitlines_impl(self, keepends);
727 :
728 1869 : exit:
729 1869 : 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 *
748 200210 : bytes_fromhex(PyTypeObject *type, PyObject *arg)
749 : {
750 200210 : PyObject *return_value = NULL;
751 : PyObject *string;
752 :
753 200210 : if (!PyUnicode_Check(arg)) {
754 2 : _PyArg_BadArgument("fromhex", "argument", "str", arg);
755 2 : goto exit;
756 : }
757 200208 : if (PyUnicode_READY(arg) == -1) {
758 0 : goto exit;
759 : }
760 200208 : string = arg;
761 200208 : return_value = bytes_fromhex_impl(type, string);
762 :
763 200210 : exit:
764 200210 : 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 *
797 58 : bytes_hex(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
798 : {
799 58 : 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 58 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
804 58 : PyObject *sep = NULL;
805 58 : int bytes_per_sep = 1;
806 :
807 58 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
808 58 : if (!args) {
809 0 : goto exit;
810 : }
811 58 : if (!noptargs) {
812 10 : goto skip_optional_pos;
813 : }
814 48 : if (args[0]) {
815 48 : sep = args[0];
816 48 : if (!--noptargs) {
817 17 : goto skip_optional_pos;
818 : }
819 : }
820 31 : bytes_per_sep = _PyLong_AsInt(args[1]);
821 31 : if (bytes_per_sep == -1 && PyErr_Occurred()) {
822 0 : goto exit;
823 : }
824 31 : skip_optional_pos:
825 58 : return_value = bytes_hex_impl(self, sep, bytes_per_sep);
826 :
827 58 : exit:
828 58 : 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 *
836 450058 : bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
837 : {
838 450058 : 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 450058 : Py_ssize_t nargs = PyTuple_GET_SIZE(args);
844 450058 : Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
845 450058 : PyObject *x = NULL;
846 450058 : const char *encoding = NULL;
847 450058 : const char *errors = NULL;
848 :
849 450058 : fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
850 450058 : if (!fastargs) {
851 0 : goto exit;
852 : }
853 450058 : if (!noptargs) {
854 119 : goto skip_optional_pos;
855 : }
856 449939 : if (fastargs[0]) {
857 449937 : x = fastargs[0];
858 449937 : if (!--noptargs) {
859 412912 : goto skip_optional_pos;
860 : }
861 : }
862 37027 : if (fastargs[1]) {
863 37023 : if (!PyUnicode_Check(fastargs[1])) {
864 1 : _PyArg_BadArgument("bytes", "argument 'encoding'", "str", fastargs[1]);
865 1 : goto exit;
866 : }
867 : Py_ssize_t encoding_length;
868 37022 : encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
869 37022 : if (encoding == NULL) {
870 0 : goto exit;
871 : }
872 37022 : if (strlen(encoding) != (size_t)encoding_length) {
873 0 : PyErr_SetString(PyExc_ValueError, "embedded null character");
874 0 : goto exit;
875 : }
876 37022 : if (!--noptargs) {
877 37014 : goto skip_optional_pos;
878 : }
879 : }
880 12 : if (!PyUnicode_Check(fastargs[2])) {
881 1 : _PyArg_BadArgument("bytes", "argument 'errors'", "str", fastargs[2]);
882 1 : goto exit;
883 : }
884 : Py_ssize_t errors_length;
885 11 : errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
886 11 : if (errors == NULL) {
887 0 : goto exit;
888 : }
889 11 : if (strlen(errors) != (size_t)errors_length) {
890 0 : PyErr_SetString(PyExc_ValueError, "embedded null character");
891 0 : goto exit;
892 : }
893 11 : skip_optional_pos:
894 450056 : return_value = bytes_new_impl(type, x, encoding, errors);
895 :
896 450058 : exit:
897 450058 : return return_value;
898 : }
899 : /*[clinic end generated code: output=5727702e63a0a8b7 input=a9049054013a1b77]*/
|