Debian Codegraph @

Objects/clinic/bytearrayobject.c.h

raw
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_abstract.h" // _PyNumber_Index()
10 #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
11 #include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
12
13 static int
14 bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
15 const char *encoding, const char *errors);
16
17 static int
18 bytearray___init__(PyObject *self, PyObject *args, PyObject *kwargs)
19 {
20 int return_value = -1;
21 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
22
23 #define NUM_KEYWORDS 3
24 static struct {
25 PyGC_Head _this_is_not_used;
26 PyObject_VAR_HEAD
27 Py_hash_t ob_hash;
28 PyObject *ob_item[NUM_KEYWORDS];
29 } _kwtuple = {
30 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
31 .ob_hash = -1,
32 .ob_item = { &_Py_ID(source), &_Py_ID(encoding), &_Py_ID(errors), },
33 };
34 #undef NUM_KEYWORDS
35 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
36
37 #else // !Py_BUILD_CORE
38 # define KWTUPLE NULL
39 #endif // !Py_BUILD_CORE
40
41 static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
42 static _PyArg_Parser _parser = {
43 .keywords = _keywords,
44 .fname = "bytearray",
45 .kwtuple = KWTUPLE,
46 };
47 #undef KWTUPLE
48 PyObject *argsbuf[3];
49 PyObject * const *fastargs;
50 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
51 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
52 PyObject *arg = NULL;
53 const char *encoding = NULL;
54 const char *errors = NULL;
55
56 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
57 /*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
58 if (!fastargs) {
59 goto exit;
60 }
61 if (!noptargs) {
62 goto skip_optional_pos;
63 }
64 if (fastargs[0]) {
65 arg = fastargs[0];
66 if (!--noptargs) {
67 goto skip_optional_pos;
68 }
69 }
70 if (fastargs[1]) {
71 if (!PyUnicode_Check(fastargs[1])) {
72 _PyArg_BadArgument("bytearray", "argument 'encoding'", "str", fastargs[1]);
73 goto exit;
74 }
75 Py_ssize_t encoding_length;
76 encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
77 if (encoding == NULL) {
78 goto exit;
79 }
80 if (strlen(encoding) != (size_t)encoding_length) {
81 PyErr_SetString(PyExc_ValueError, "embedded null character");
82 goto exit;
83 }
84 if (!--noptargs) {
85 goto skip_optional_pos;
86 }
87 }
88 if (!PyUnicode_Check(fastargs[2])) {
89 _PyArg_BadArgument("bytearray", "argument 'errors'", "str", fastargs[2]);
90 goto exit;
91 }
92 Py_ssize_t errors_length;
93 errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
94 if (errors == NULL) {
95 goto exit;
96 }
97 if (strlen(errors) != (size_t)errors_length) {
98 PyErr_SetString(PyExc_ValueError, "embedded null character");
99 goto exit;
100 }
101 skip_optional_pos:
102 return_value = bytearray___init___impl((PyByteArrayObject *)self, arg, encoding, errors);
103
104 exit:
105 return return_value;
106 }
107
108 PyDoc_STRVAR(bytearray_find__doc__,
109 "find($self, sub[, start[, end]], /)\n"
110 "--\n"
111 "\n"
112 "Return the lowest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n"
113 "\n"
114 " start\n"
115 " Optional start position. Default: start of the bytes.\n"
116 " end\n"
117 " Optional stop position. Default: end of the bytes.\n"
118 "\n"
119 "Return -1 on failure.");
120
121 #define BYTEARRAY_FIND_METHODDEF \
122 {"find", _PyCFunction_CAST(bytearray_find), METH_FASTCALL, bytearray_find__doc__},
123
124 static PyObject *
125 bytearray_find_impl(PyByteArrayObject *self, PyObject *sub, Py_ssize_t start,
126 Py_ssize_t end);
127
128 static PyObject *
129 bytearray_find(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
130 {
131 PyObject *return_value = NULL;
132 PyObject *sub;
133 Py_ssize_t start = 0;
134 Py_ssize_t end = PY_SSIZE_T_MAX;
135
136 if (!_PyArg_CheckPositional("find", nargs, 1, 3)) {
137 goto exit;
138 }
139 sub = args[0];
140 if (nargs < 2) {
141 goto skip_optional;
142 }
143 if (!_PyEval_SliceIndex(args[1], &start)) {
144 goto exit;
145 }
146 if (nargs < 3) {
147 goto skip_optional;
148 }
149 if (!_PyEval_SliceIndex(args[2], &end)) {
150 goto exit;
151 }
152 skip_optional:
153 Py_BEGIN_CRITICAL_SECTION(self);
154 return_value = bytearray_find_impl((PyByteArrayObject *)self, sub, start, end);
155 Py_END_CRITICAL_SECTION();
156
157 exit:
158 return return_value;
159 }
160
161 PyDoc_STRVAR(bytearray_count__doc__,
162 "count($self, sub[, start[, end]], /)\n"
163 "--\n"
164 "\n"
165 "Return the number of non-overlapping occurrences of subsection \'sub\' in bytes B[start:end].\n"
166 "\n"
167 " start\n"
168 " Optional start position. Default: start of the bytes.\n"
169 " end\n"
170 " Optional stop position. Default: end of the bytes.");
171
172 #define BYTEARRAY_COUNT_METHODDEF \
173 {"count", _PyCFunction_CAST(bytearray_count), METH_FASTCALL, bytearray_count__doc__},
174
175 static PyObject *
176 bytearray_count_impl(PyByteArrayObject *self, PyObject *sub,
177 Py_ssize_t start, Py_ssize_t end);
178
179 static PyObject *
180 bytearray_count(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
181 {
182 PyObject *return_value = NULL;
183 PyObject *sub;
184 Py_ssize_t start = 0;
185 Py_ssize_t end = PY_SSIZE_T_MAX;
186
187 if (!_PyArg_CheckPositional("count", nargs, 1, 3)) {
188 goto exit;
189 }
190 sub = args[0];
191 if (nargs < 2) {
192 goto skip_optional;
193 }
194 if (!_PyEval_SliceIndex(args[1], &start)) {
195 goto exit;
196 }
197 if (nargs < 3) {
198 goto skip_optional;
199 }
200 if (!_PyEval_SliceIndex(args[2], &end)) {
201 goto exit;
202 }
203 skip_optional:
204 Py_BEGIN_CRITICAL_SECTION(self);
205 return_value = bytearray_count_impl((PyByteArrayObject *)self, sub, start, end);
206 Py_END_CRITICAL_SECTION();
207
208 exit:
209 return return_value;
210 }
211
212 PyDoc_STRVAR(bytearray_clear__doc__,
213 "clear($self, /)\n"
214 "--\n"
215 "\n"
216 "Remove all items from the bytearray.");
217
218 #define BYTEARRAY_CLEAR_METHODDEF \
219 {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, bytearray_clear__doc__},
220
221 static PyObject *
222 bytearray_clear_impl(PyByteArrayObject *self);
223
224 static PyObject *
225 bytearray_clear(PyObject *self, PyObject *Py_UNUSED(ignored))
226 {
227 return bytearray_clear_impl((PyByteArrayObject *)self);
228 }
229
230 PyDoc_STRVAR(bytearray_copy__doc__,
231 "copy($self, /)\n"
232 "--\n"
233 "\n"
234 "Return a copy of B.");
235
236 #define BYTEARRAY_COPY_METHODDEF \
237 {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, bytearray_copy__doc__},
238
239 static PyObject *
240 bytearray_copy_impl(PyByteArrayObject *self);
241
242 static PyObject *
243 bytearray_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
244 {
245 PyObject *return_value = NULL;
246
247 Py_BEGIN_CRITICAL_SECTION(self);
248 return_value = bytearray_copy_impl((PyByteArrayObject *)self);
249 Py_END_CRITICAL_SECTION();
250
251 return return_value;
252 }
253
254 PyDoc_STRVAR(bytearray_index__doc__,
255 "index($self, sub[, start[, end]], /)\n"
256 "--\n"
257 "\n"
258 "Return the lowest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n"
259 "\n"
260 " start\n"
261 " Optional start position. Default: start of the bytes.\n"
262 " end\n"
263 " Optional stop position. Default: end of the bytes.\n"
264 "\n"
265 "Raise ValueError if the subsection is not found.");
266
267 #define BYTEARRAY_INDEX_METHODDEF \
268 {"index", _PyCFunction_CAST(bytearray_index), METH_FASTCALL, bytearray_index__doc__},
269
270 static PyObject *
271 bytearray_index_impl(PyByteArrayObject *self, PyObject *sub,
272 Py_ssize_t start, Py_ssize_t end);
273
274 static PyObject *
275 bytearray_index(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
276 {
277 PyObject *return_value = NULL;
278 PyObject *sub;
279 Py_ssize_t start = 0;
280 Py_ssize_t end = PY_SSIZE_T_MAX;
281
282 if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
283 goto exit;
284 }
285 sub = args[0];
286 if (nargs < 2) {
287 goto skip_optional;
288 }
289 if (!_PyEval_SliceIndex(args[1], &start)) {
290 goto exit;
291 }
292 if (nargs < 3) {
293 goto skip_optional;
294 }
295 if (!_PyEval_SliceIndex(args[2], &end)) {
296 goto exit;
297 }
298 skip_optional:
299 Py_BEGIN_CRITICAL_SECTION(self);
300 return_value = bytearray_index_impl((PyByteArrayObject *)self, sub, start, end);
301 Py_END_CRITICAL_SECTION();
302
303 exit:
304 return return_value;
305 }
306
307 PyDoc_STRVAR(bytearray_rfind__doc__,
308 "rfind($self, sub[, start[, end]], /)\n"
309 "--\n"
310 "\n"
311 "Return the highest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n"
312 "\n"
313 " start\n"
314 " Optional start position. Default: start of the bytes.\n"
315 " end\n"
316 " Optional stop position. Default: end of the bytes.\n"
317 "\n"
318 "Return -1 on failure.");
319
320 #define BYTEARRAY_RFIND_METHODDEF \
321 {"rfind", _PyCFunction_CAST(bytearray_rfind), METH_FASTCALL, bytearray_rfind__doc__},
322
323 static PyObject *
324 bytearray_rfind_impl(PyByteArrayObject *self, PyObject *sub,
325 Py_ssize_t start, Py_ssize_t end);
326
327 static PyObject *
328 bytearray_rfind(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
329 {
330 PyObject *return_value = NULL;
331 PyObject *sub;
332 Py_ssize_t start = 0;
333 Py_ssize_t end = PY_SSIZE_T_MAX;
334
335 if (!_PyArg_CheckPositional("rfind", nargs, 1, 3)) {
336 goto exit;
337 }
338 sub = args[0];
339 if (nargs < 2) {
340 goto skip_optional;
341 }
342 if (!_PyEval_SliceIndex(args[1], &start)) {
343 goto exit;
344 }
345 if (nargs < 3) {
346 goto skip_optional;
347 }
348 if (!_PyEval_SliceIndex(args[2], &end)) {
349 goto exit;
350 }
351 skip_optional:
352 Py_BEGIN_CRITICAL_SECTION(self);
353 return_value = bytearray_rfind_impl((PyByteArrayObject *)self, sub, start, end);
354 Py_END_CRITICAL_SECTION();
355
356 exit:
357 return return_value;
358 }
359
360 PyDoc_STRVAR(bytearray_rindex__doc__,
361 "rindex($self, sub[, start[, end]], /)\n"
362 "--\n"
363 "\n"
364 "Return the highest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n"
365 "\n"
366 " start\n"
367 " Optional start position. Default: start of the bytes.\n"
368 " end\n"
369 " Optional stop position. Default: end of the bytes.\n"
370 "\n"
371 "Raise ValueError if the subsection is not found.");
372
373 #define BYTEARRAY_RINDEX_METHODDEF \
374 {"rindex", _PyCFunction_CAST(bytearray_rindex), METH_FASTCALL, bytearray_rindex__doc__},
375
376 static PyObject *
377 bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub,
378 Py_ssize_t start, Py_ssize_t end);
379
380 static PyObject *
381 bytearray_rindex(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
382 {
383 PyObject *return_value = NULL;
384 PyObject *sub;
385 Py_ssize_t start = 0;
386 Py_ssize_t end = PY_SSIZE_T_MAX;
387
388 if (!_PyArg_CheckPositional("rindex", nargs, 1, 3)) {
389 goto exit;
390 }
391 sub = args[0];
392 if (nargs < 2) {
393 goto skip_optional;
394 }
395 if (!_PyEval_SliceIndex(args[1], &start)) {
396 goto exit;
397 }
398 if (nargs < 3) {
399 goto skip_optional;
400 }
401 if (!_PyEval_SliceIndex(args[2], &end)) {
402 goto exit;
403 }
404 skip_optional:
405 Py_BEGIN_CRITICAL_SECTION(self);
406 return_value = bytearray_rindex_impl((PyByteArrayObject *)self, sub, start, end);
407 Py_END_CRITICAL_SECTION();
408
409 exit:
410 return return_value;
411 }
412
413 PyDoc_STRVAR(bytearray_startswith__doc__,
414 "startswith($self, prefix[, start[, end]], /)\n"
415 "--\n"
416 "\n"
417 "Return True if the bytearray starts with the specified prefix, False otherwise.\n"
418 "\n"
419 " prefix\n"
420 " A bytes or a tuple of bytes to try.\n"
421 " start\n"
422 " Optional start position. Default: start of the bytearray.\n"
423 " end\n"
424 " Optional stop position. Default: end of the bytearray.");
425
426 #define BYTEARRAY_STARTSWITH_METHODDEF \
427 {"startswith", _PyCFunction_CAST(bytearray_startswith), METH_FASTCALL, bytearray_startswith__doc__},
428
429 static PyObject *
430 bytearray_startswith_impl(PyByteArrayObject *self, PyObject *subobj,
431 Py_ssize_t start, Py_ssize_t end);
432
433 static PyObject *
434 bytearray_startswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
435 {
436 PyObject *return_value = NULL;
437 PyObject *subobj;
438 Py_ssize_t start = 0;
439 Py_ssize_t end = PY_SSIZE_T_MAX;
440
441 if (!_PyArg_CheckPositional("startswith", nargs, 1, 3)) {
442 goto exit;
443 }
444 subobj = args[0];
445 if (nargs < 2) {
446 goto skip_optional;
447 }
448 if (!_PyEval_SliceIndex(args[1], &start)) {
449 goto exit;
450 }
451 if (nargs < 3) {
452 goto skip_optional;
453 }
454 if (!_PyEval_SliceIndex(args[2], &end)) {
455 goto exit;
456 }
457 skip_optional:
458 Py_BEGIN_CRITICAL_SECTION(self);
459 return_value = bytearray_startswith_impl((PyByteArrayObject *)self, subobj, start, end);
460 Py_END_CRITICAL_SECTION();
461
462 exit:
463 return return_value;
464 }
465
466 PyDoc_STRVAR(bytearray_endswith__doc__,
467 "endswith($self, suffix[, start[, end]], /)\n"
468 "--\n"
469 "\n"
470 "Return True if the bytearray ends with the specified suffix, False otherwise.\n"
471 "\n"
472 " suffix\n"
473 " A bytes or a tuple of bytes to try.\n"
474 " start\n"
475 " Optional start position. Default: start of the bytearray.\n"
476 " end\n"
477 " Optional stop position. Default: end of the bytearray.");
478
479 #define BYTEARRAY_ENDSWITH_METHODDEF \
480 {"endswith", _PyCFunction_CAST(bytearray_endswith), METH_FASTCALL, bytearray_endswith__doc__},
481
482 static PyObject *
483 bytearray_endswith_impl(PyByteArrayObject *self, PyObject *subobj,
484 Py_ssize_t start, Py_ssize_t end);
485
486 static PyObject *
487 bytearray_endswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
488 {
489 PyObject *return_value = NULL;
490 PyObject *subobj;
491 Py_ssize_t start = 0;
492 Py_ssize_t end = PY_SSIZE_T_MAX;
493
494 if (!_PyArg_CheckPositional("endswith", nargs, 1, 3)) {
495 goto exit;
496 }
497 subobj = args[0];
498 if (nargs < 2) {
499 goto skip_optional;
500 }
501 if (!_PyEval_SliceIndex(args[1], &start)) {
502 goto exit;
503 }
504 if (nargs < 3) {
505 goto skip_optional;
506 }
507 if (!_PyEval_SliceIndex(args[2], &end)) {
508 goto exit;
509 }
510 skip_optional:
511 Py_BEGIN_CRITICAL_SECTION(self);
512 return_value = bytearray_endswith_impl((PyByteArrayObject *)self, subobj, start, end);
513 Py_END_CRITICAL_SECTION();
514
515 exit:
516 return return_value;
517 }
518
519 PyDoc_STRVAR(bytearray_removeprefix__doc__,
520 "removeprefix($self, prefix, /)\n"
521 "--\n"
522 "\n"
523 "Return a bytearray with the given prefix string removed if present.\n"
524 "\n"
525 "If the bytearray starts with the prefix string, return\n"
526 "bytearray[len(prefix):]. Otherwise, return a copy of the original\n"
527 "bytearray.");
528
529 #define BYTEARRAY_REMOVEPREFIX_METHODDEF \
530 {"removeprefix", (PyCFunction)bytearray_removeprefix, METH_O, bytearray_removeprefix__doc__},
531
532 static PyObject *
533 bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix);
534
535 static PyObject *
536 bytearray_removeprefix(PyObject *self, PyObject *arg)
537 {
538 PyObject *return_value = NULL;
539 Py_buffer prefix = {NULL, NULL};
540
541 if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
542 goto exit;
543 }
544 Py_BEGIN_CRITICAL_SECTION(self);
545 return_value = bytearray_removeprefix_impl((PyByteArrayObject *)self, &prefix);
546 Py_END_CRITICAL_SECTION();
547
548 exit:
549 /* Cleanup for prefix */
550 if (prefix.obj) {
551 PyBuffer_Release(&prefix);
552 }
553
554 return return_value;
555 }
556
557 PyDoc_STRVAR(bytearray_removesuffix__doc__,
558 "removesuffix($self, suffix, /)\n"
559 "--\n"
560 "\n"
561 "Return a bytearray with the given suffix string removed if present.\n"
562 "\n"
563 "If the bytearray ends with the suffix string and that suffix is not\n"
564 "empty, return bytearray[:-len(suffix)]. Otherwise, return a copy of\n"
565 "the original bytearray.");
566
567 #define BYTEARRAY_REMOVESUFFIX_METHODDEF \
568 {"removesuffix", (PyCFunction)bytearray_removesuffix, METH_O, bytearray_removesuffix__doc__},
569
570 static PyObject *
571 bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix);
572
573 static PyObject *
574 bytearray_removesuffix(PyObject *self, PyObject *arg)
575 {
576 PyObject *return_value = NULL;
577 Py_buffer suffix = {NULL, NULL};
578
579 if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
580 goto exit;
581 }
582 Py_BEGIN_CRITICAL_SECTION(self);
583 return_value = bytearray_removesuffix_impl((PyByteArrayObject *)self, &suffix);
584 Py_END_CRITICAL_SECTION();
585
586 exit:
587 /* Cleanup for suffix */
588 if (suffix.obj) {
589 PyBuffer_Release(&suffix);
590 }
591
592 return return_value;
593 }
594
595 PyDoc_STRVAR(bytearray_resize__doc__,
596 "resize($self, size, /)\n"
597 "--\n"
598 "\n"
599 "Resize the internal buffer of bytearray to len.\n"
600 "\n"
601 " size\n"
602 " New size to resize to.");
603
604 #define BYTEARRAY_RESIZE_METHODDEF \
605 {"resize", (PyCFunction)bytearray_resize, METH_O, bytearray_resize__doc__},
606
607 static PyObject *
608 bytearray_resize_impl(PyByteArrayObject *self, Py_ssize_t size);
609
610 static PyObject *
611 bytearray_resize(PyObject *self, PyObject *arg)
612 {
613 PyObject *return_value = NULL;
614 Py_ssize_t size;
615
616 {
617 Py_ssize_t ival = -1;
618 PyObject *iobj = _PyNumber_Index(arg);
619 if (iobj != NULL) {
620 ival = PyLong_AsSsize_t(iobj);
621 Py_DECREF(iobj);
622 }
623 if (ival == -1 && PyErr_Occurred()) {
624 goto exit;
625 }
626 size = ival;
627 }
628 Py_BEGIN_CRITICAL_SECTION(self);
629 return_value = bytearray_resize_impl((PyByteArrayObject *)self, size);
630 Py_END_CRITICAL_SECTION();
631
632 exit:
633 return return_value;
634 }
635
636 PyDoc_STRVAR(bytearray_translate__doc__,
637 "translate($self, table, /, delete=b\'\')\n"
638 "--\n"
639 "\n"
640 "Return a copy with each character mapped by the given translation table.\n"
641 "\n"
642 " table\n"
643 " Translation table, which must be a bytes object of length 256.\n"
644 "\n"
645 "All characters occurring in the optional argument delete are\n"
646 "removed. The remaining characters are mapped through the given\n"
647 "translation table.");
648
649 #define BYTEARRAY_TRANSLATE_METHODDEF \
650 {"translate", _PyCFunction_CAST(bytearray_translate), METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__},
651
652 static PyObject *
653 bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
654 PyObject *deletechars);
655
656 static PyObject *
657 bytearray_translate(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
658 {
659 PyObject *return_value = NULL;
660 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
661
662 #define NUM_KEYWORDS 1
663 static struct {
664 PyGC_Head _this_is_not_used;
665 PyObject_VAR_HEAD
666 Py_hash_t ob_hash;
667 PyObject *ob_item[NUM_KEYWORDS];
668 } _kwtuple = {
669 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
670 .ob_hash = -1,
671 .ob_item = { &_Py_ID(delete), },
672 };
673 #undef NUM_KEYWORDS
674 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
675
676 #else // !Py_BUILD_CORE
677 # define KWTUPLE NULL
678 #endif // !Py_BUILD_CORE
679
680 static const char * const _keywords[] = {"", "delete", NULL};
681 static _PyArg_Parser _parser = {
682 .keywords = _keywords,
683 .fname = "translate",
684 .kwtuple = KWTUPLE,
685 };
686 #undef KWTUPLE
687 PyObject *argsbuf[2];
688 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
689 PyObject *table;
690 PyObject *deletechars = NULL;
691
692 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
693 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
694 if (!args) {
695 goto exit;
696 }
697 table = args[0];
698 if (!noptargs) {
699 goto skip_optional_pos;
700 }
701 deletechars = args[1];
702 skip_optional_pos:
703 Py_BEGIN_CRITICAL_SECTION(self);
704 return_value = bytearray_translate_impl((PyByteArrayObject *)self, table, deletechars);
705 Py_END_CRITICAL_SECTION();
706
707 exit:
708 return return_value;
709 }
710
711 PyDoc_STRVAR(bytearray_maketrans__doc__,
712 "maketrans(frm, to, /)\n"
713 "--\n"
714 "\n"
715 "Return a translation table usable for the bytes or bytearray translate method.\n"
716 "\n"
717 "The returned table will be one where each byte in frm is mapped to\n"
718 "the byte at the same position in to.\n"
719 "\n"
720 "The bytes objects frm and to must be of the same length.");
721
722 #define BYTEARRAY_MAKETRANS_METHODDEF \
723 {"maketrans", _PyCFunction_CAST(bytearray_maketrans), METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__},
724
725 static PyObject *
726 bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
727
728 static PyObject *
729 bytearray_maketrans(PyObject *null, PyObject *const *args, Py_ssize_t nargs)
730 {
731 PyObject *return_value = NULL;
732 Py_buffer frm = {NULL, NULL};
733 Py_buffer to = {NULL, NULL};
734
735 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
736 goto exit;
737 }
738 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
739 goto exit;
740 }
741 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
742 goto exit;
743 }
744 return_value = bytearray_maketrans_impl(&frm, &to);
745
746 exit:
747 /* Cleanup for frm */
748 if (frm.obj) {
749 PyBuffer_Release(&frm);
750 }
751 /* Cleanup for to */
752 if (to.obj) {
753 PyBuffer_Release(&to);
754 }
755
756 return return_value;
757 }
758
759 PyDoc_STRVAR(bytearray_replace__doc__,
760 "replace($self, old, new, count=-1, /)\n"
761 "--\n"
762 "\n"
763 "Return a copy with all occurrences of substring old replaced by new.\n"
764 "\n"
765 " count\n"
766 " Maximum number of occurrences to replace.\n"
767 " -1 (the default value) means replace all occurrences.\n"
768 "\n"
769 "If the optional argument count is given, only the first count occurrences are\n"
770 "replaced.");
771
772 #define BYTEARRAY_REPLACE_METHODDEF \
773 {"replace", _PyCFunction_CAST(bytearray_replace), METH_FASTCALL, bytearray_replace__doc__},
774
775 static PyObject *
776 bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
777 Py_buffer *new, Py_ssize_t count);
778
779 static PyObject *
780 bytearray_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
781 {
782 PyObject *return_value = NULL;
783 Py_buffer old = {NULL, NULL};
784 Py_buffer new = {NULL, NULL};
785 Py_ssize_t count = -1;
786
787 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
788 goto exit;
789 }
790 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
791 goto exit;
792 }
793 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
794 goto exit;
795 }
796 if (nargs < 3) {
797 goto skip_optional;
798 }
799 {
800 Py_ssize_t ival = -1;
801 PyObject *iobj = _PyNumber_Index(args[2]);
802 if (iobj != NULL) {
803 ival = PyLong_AsSsize_t(iobj);
804 Py_DECREF(iobj);
805 }
806 if (ival == -1 && PyErr_Occurred()) {
807 goto exit;
808 }
809 count = ival;
810 }
811 skip_optional:
812 Py_BEGIN_CRITICAL_SECTION(self);
813 return_value = bytearray_replace_impl((PyByteArrayObject *)self, &old, &new, count);
814 Py_END_CRITICAL_SECTION();
815
816 exit:
817 /* Cleanup for old */
818 if (old.obj) {
819 PyBuffer_Release(&old);
820 }
821 /* Cleanup for new */
822 if (new.obj) {
823 PyBuffer_Release(&new);
824 }
825
826 return return_value;
827 }
828
829 PyDoc_STRVAR(bytearray_split__doc__,
830 "split($self, /, sep=None, maxsplit=-1)\n"
831 "--\n"
832 "\n"
833 "Return a list of the sections in the bytearray, using sep as the delimiter.\n"
834 "\n"
835 " sep\n"
836 " The delimiter according which to split the bytearray.\n"
837 " None (the default value) means split on ASCII whitespace\n"
838 " characters (space, tab, return, newline, formfeed, vertical tab).\n"
839 " maxsplit\n"
840 " Maximum number of splits to do.\n"
841 " -1 (the default value) means no limit.");
842
843 #define BYTEARRAY_SPLIT_METHODDEF \
844 {"split", _PyCFunction_CAST(bytearray_split), METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
845
846 static PyObject *
847 bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
848 Py_ssize_t maxsplit);
849
850 static PyObject *
851 bytearray_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
852 {
853 PyObject *return_value = NULL;
854 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
855
856 #define NUM_KEYWORDS 2
857 static struct {
858 PyGC_Head _this_is_not_used;
859 PyObject_VAR_HEAD
860 Py_hash_t ob_hash;
861 PyObject *ob_item[NUM_KEYWORDS];
862 } _kwtuple = {
863 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
864 .ob_hash = -1,
865 .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
866 };
867 #undef NUM_KEYWORDS
868 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
869
870 #else // !Py_BUILD_CORE
871 # define KWTUPLE NULL
872 #endif // !Py_BUILD_CORE
873
874 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
875 static _PyArg_Parser _parser = {
876 .keywords = _keywords,
877 .fname = "split",
878 .kwtuple = KWTUPLE,
879 };
880 #undef KWTUPLE
881 PyObject *argsbuf[2];
882 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
883 PyObject *sep = Py_None;
884 Py_ssize_t maxsplit = -1;
885
886 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
887 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
888 if (!args) {
889 goto exit;
890 }
891 if (!noptargs) {
892 goto skip_optional_pos;
893 }
894 if (args[0]) {
895 sep = args[0];
896 if (!--noptargs) {
897 goto skip_optional_pos;
898 }
899 }
900 {
901 Py_ssize_t ival = -1;
902 PyObject *iobj = _PyNumber_Index(args[1]);
903 if (iobj != NULL) {
904 ival = PyLong_AsSsize_t(iobj);
905 Py_DECREF(iobj);
906 }
907 if (ival == -1 && PyErr_Occurred()) {
908 goto exit;
909 }
910 maxsplit = ival;
911 }
912 skip_optional_pos:
913 Py_BEGIN_CRITICAL_SECTION(self);
914 return_value = bytearray_split_impl((PyByteArrayObject *)self, sep, maxsplit);
915 Py_END_CRITICAL_SECTION();
916
917 exit:
918 return return_value;
919 }
920
921 PyDoc_STRVAR(bytearray_partition__doc__,
922 "partition($self, sep, /)\n"
923 "--\n"
924 "\n"
925 "Partition the bytearray into three parts using the given separator.\n"
926 "\n"
927 "This will search for the separator sep in the bytearray. If the\n"
928 "separator is found, returns a 3-tuple containing the part before the\n"
929 "separator, the separator itself, and the part after it as new\n"
930 "bytearray objects.\n"
931 "\n"
932 "If the separator is not found, returns a 3-tuple containing the copy\n"
933 "of the original bytearray object and two empty bytearray objects.");
934
935 #define BYTEARRAY_PARTITION_METHODDEF \
936 {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
937
938 static PyObject *
939 bytearray_partition_impl(PyByteArrayObject *self, PyObject *sep);
940
941 static PyObject *
942 bytearray_partition(PyObject *self, PyObject *sep)
943 {
944 PyObject *return_value = NULL;
945
946 Py_BEGIN_CRITICAL_SECTION(self);
947 return_value = bytearray_partition_impl((PyByteArrayObject *)self, sep);
948 Py_END_CRITICAL_SECTION();
949
950 return return_value;
951 }
952
953 PyDoc_STRVAR(bytearray_rpartition__doc__,
954 "rpartition($self, sep, /)\n"
955 "--\n"
956 "\n"
957 "Partition the bytearray into three parts using the given separator.\n"
958 "\n"
959 "This will search for the separator sep in the bytearray, starting at\n"
960 "the end. If the separator is found, returns a 3-tuple containing\n"
961 "the part before the separator, the separator itself, and the part\n"
962 "after it as new bytearray objects.\n"
963 "\n"
964 "If the separator is not found, returns a 3-tuple containing two\n"
965 "empty bytearray objects and the copy of the original bytearray\n"
966 "object.");
967
968 #define BYTEARRAY_RPARTITION_METHODDEF \
969 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
970
971 static PyObject *
972 bytearray_rpartition_impl(PyByteArrayObject *self, PyObject *sep);
973
974 static PyObject *
975 bytearray_rpartition(PyObject *self, PyObject *sep)
976 {
977 PyObject *return_value = NULL;
978
979 Py_BEGIN_CRITICAL_SECTION(self);
980 return_value = bytearray_rpartition_impl((PyByteArrayObject *)self, sep);
981 Py_END_CRITICAL_SECTION();
982
983 return return_value;
984 }
985
986 PyDoc_STRVAR(bytearray_rsplit__doc__,
987 "rsplit($self, /, sep=None, maxsplit=-1)\n"
988 "--\n"
989 "\n"
990 "Return a list of the sections in the bytearray, using sep as the delimiter.\n"
991 "\n"
992 " sep\n"
993 " The delimiter according which to split the bytearray.\n"
994 " None (the default value) means split on ASCII whitespace\n"
995 " characters (space, tab, return, newline, formfeed, vertical tab).\n"
996 " maxsplit\n"
997 " Maximum number of splits to do.\n"
998 " -1 (the default value) means no limit.\n"
999 "\n"
1000 "Splitting is done starting at the end of the bytearray and working\n"
1001 "to the front.");
1002
1003 #define BYTEARRAY_RSPLIT_METHODDEF \
1004 {"rsplit", _PyCFunction_CAST(bytearray_rsplit), METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
1005
1006 static PyObject *
1007 bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1008 Py_ssize_t maxsplit);
1009
1010 static PyObject *
1011 bytearray_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1012 {
1013 PyObject *return_value = NULL;
1014 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1015
1016 #define NUM_KEYWORDS 2
1017 static struct {
1018 PyGC_Head _this_is_not_used;
1019 PyObject_VAR_HEAD
1020 Py_hash_t ob_hash;
1021 PyObject *ob_item[NUM_KEYWORDS];
1022 } _kwtuple = {
1023 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1024 .ob_hash = -1,
1025 .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
1026 };
1027 #undef NUM_KEYWORDS
1028 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1029
1030 #else // !Py_BUILD_CORE
1031 # define KWTUPLE NULL
1032 #endif // !Py_BUILD_CORE
1033
1034 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
1035 static _PyArg_Parser _parser = {
1036 .keywords = _keywords,
1037 .fname = "rsplit",
1038 .kwtuple = KWTUPLE,
1039 };
1040 #undef KWTUPLE
1041 PyObject *argsbuf[2];
1042 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1043 PyObject *sep = Py_None;
1044 Py_ssize_t maxsplit = -1;
1045
1046 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1047 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1048 if (!args) {
1049 goto exit;
1050 }
1051 if (!noptargs) {
1052 goto skip_optional_pos;
1053 }
1054 if (args[0]) {
1055 sep = args[0];
1056 if (!--noptargs) {
1057 goto skip_optional_pos;
1058 }
1059 }
1060 {
1061 Py_ssize_t ival = -1;
1062 PyObject *iobj = _PyNumber_Index(args[1]);
1063 if (iobj != NULL) {
1064 ival = PyLong_AsSsize_t(iobj);
1065 Py_DECREF(iobj);
1066 }
1067 if (ival == -1 && PyErr_Occurred()) {
1068 goto exit;
1069 }
1070 maxsplit = ival;
1071 }
1072 skip_optional_pos:
1073 Py_BEGIN_CRITICAL_SECTION(self);
1074 return_value = bytearray_rsplit_impl((PyByteArrayObject *)self, sep, maxsplit);
1075 Py_END_CRITICAL_SECTION();
1076
1077 exit:
1078 return return_value;
1079 }
1080
1081 PyDoc_STRVAR(bytearray_reverse__doc__,
1082 "reverse($self, /)\n"
1083 "--\n"
1084 "\n"
1085 "Reverse the order of the values in B in place.");
1086
1087 #define BYTEARRAY_REVERSE_METHODDEF \
1088 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
1089
1090 static PyObject *
1091 bytearray_reverse_impl(PyByteArrayObject *self);
1092
1093 static PyObject *
1094 bytearray_reverse(PyObject *self, PyObject *Py_UNUSED(ignored))
1095 {
1096 PyObject *return_value = NULL;
1097
1098 Py_BEGIN_CRITICAL_SECTION(self);
1099 return_value = bytearray_reverse_impl((PyByteArrayObject *)self);
1100 Py_END_CRITICAL_SECTION();
1101
1102 return return_value;
1103 }
1104
1105 PyDoc_STRVAR(bytearray_insert__doc__,
1106 "insert($self, index, item, /)\n"
1107 "--\n"
1108 "\n"
1109 "Insert a single item into the bytearray before the given index.\n"
1110 "\n"
1111 " index\n"
1112 " The index where the value is to be inserted.\n"
1113 " item\n"
1114 " The item to be inserted.");
1115
1116 #define BYTEARRAY_INSERT_METHODDEF \
1117 {"insert", _PyCFunction_CAST(bytearray_insert), METH_FASTCALL, bytearray_insert__doc__},
1118
1119 static PyObject *
1120 bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
1121
1122 static PyObject *
1123 bytearray_insert(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1124 {
1125 PyObject *return_value = NULL;
1126 Py_ssize_t index;
1127 int item;
1128
1129 if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
1130 goto exit;
1131 }
1132 {
1133 Py_ssize_t ival = -1;
1134 PyObject *iobj = _PyNumber_Index(args[0]);
1135 if (iobj != NULL) {
1136 ival = PyLong_AsSsize_t(iobj);
1137 Py_DECREF(iobj);
1138 }
1139 if (ival == -1 && PyErr_Occurred()) {
1140 goto exit;
1141 }
1142 index = ival;
1143 }
1144 if (!_getbytevalue(args[1], &item)) {
1145 goto exit;
1146 }
1147 Py_BEGIN_CRITICAL_SECTION(self);
1148 return_value = bytearray_insert_impl((PyByteArrayObject *)self, index, item);
1149 Py_END_CRITICAL_SECTION();
1150
1151 exit:
1152 return return_value;
1153 }
1154
1155 PyDoc_STRVAR(bytearray_append__doc__,
1156 "append($self, item, /)\n"
1157 "--\n"
1158 "\n"
1159 "Append a single item to the end of the bytearray.\n"
1160 "\n"
1161 " item\n"
1162 " The item to be appended.");
1163
1164 #define BYTEARRAY_APPEND_METHODDEF \
1165 {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
1166
1167 static PyObject *
1168 bytearray_append_impl(PyByteArrayObject *self, int item);
1169
1170 static PyObject *
1171 bytearray_append(PyObject *self, PyObject *arg)
1172 {
1173 PyObject *return_value = NULL;
1174 int item;
1175
1176 if (!_getbytevalue(arg, &item)) {
1177 goto exit;
1178 }
1179 Py_BEGIN_CRITICAL_SECTION(self);
1180 return_value = bytearray_append_impl((PyByteArrayObject *)self, item);
1181 Py_END_CRITICAL_SECTION();
1182
1183 exit:
1184 return return_value;
1185 }
1186
1187 PyDoc_STRVAR(bytearray_extend__doc__,
1188 "extend($self, iterable_of_ints, /)\n"
1189 "--\n"
1190 "\n"
1191 "Append all the items from the iterator or sequence to the end of the bytearray.\n"
1192 "\n"
1193 " iterable_of_ints\n"
1194 " The iterable of items to append.");
1195
1196 #define BYTEARRAY_EXTEND_METHODDEF \
1197 {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
1198
1199 static PyObject *
1200 bytearray_extend_impl(PyByteArrayObject *self, PyObject *iterable_of_ints);
1201
1202 static PyObject *
1203 bytearray_extend(PyObject *self, PyObject *iterable_of_ints)
1204 {
1205 PyObject *return_value = NULL;
1206
1207 Py_BEGIN_CRITICAL_SECTION(self);
1208 return_value = bytearray_extend_impl((PyByteArrayObject *)self, iterable_of_ints);
1209 Py_END_CRITICAL_SECTION();
1210
1211 return return_value;
1212 }
1213
1214 PyDoc_STRVAR(bytearray_pop__doc__,
1215 "pop($self, index=-1, /)\n"
1216 "--\n"
1217 "\n"
1218 "Remove and return a single item from B.\n"
1219 "\n"
1220 " index\n"
1221 " The index from where to remove the item.\n"
1222 " -1 (the default value) means remove the last item.\n"
1223 "\n"
1224 "If no index argument is given, will pop the last item.");
1225
1226 #define BYTEARRAY_POP_METHODDEF \
1227 {"pop", _PyCFunction_CAST(bytearray_pop), METH_FASTCALL, bytearray_pop__doc__},
1228
1229 static PyObject *
1230 bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
1231
1232 static PyObject *
1233 bytearray_pop(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1234 {
1235 PyObject *return_value = NULL;
1236 Py_ssize_t index = -1;
1237
1238 if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
1239 goto exit;
1240 }
1241 if (nargs < 1) {
1242 goto skip_optional;
1243 }
1244 {
1245 Py_ssize_t ival = -1;
1246 PyObject *iobj = _PyNumber_Index(args[0]);
1247 if (iobj != NULL) {
1248 ival = PyLong_AsSsize_t(iobj);
1249 Py_DECREF(iobj);
1250 }
1251 if (ival == -1 && PyErr_Occurred()) {
1252 goto exit;
1253 }
1254 index = ival;
1255 }
1256 skip_optional:
1257 Py_BEGIN_CRITICAL_SECTION(self);
1258 return_value = bytearray_pop_impl((PyByteArrayObject *)self, index);
1259 Py_END_CRITICAL_SECTION();
1260
1261 exit:
1262 return return_value;
1263 }
1264
1265 PyDoc_STRVAR(bytearray_remove__doc__,
1266 "remove($self, value, /)\n"
1267 "--\n"
1268 "\n"
1269 "Remove the first occurrence of a value in the bytearray.\n"
1270 "\n"
1271 " value\n"
1272 " The value to remove.");
1273
1274 #define BYTEARRAY_REMOVE_METHODDEF \
1275 {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
1276
1277 static PyObject *
1278 bytearray_remove_impl(PyByteArrayObject *self, int value);
1279
1280 static PyObject *
1281 bytearray_remove(PyObject *self, PyObject *arg)
1282 {
1283 PyObject *return_value = NULL;
1284 int value;
1285
1286 if (!_getbytevalue(arg, &value)) {
1287 goto exit;
1288 }
1289 Py_BEGIN_CRITICAL_SECTION(self);
1290 return_value = bytearray_remove_impl((PyByteArrayObject *)self, value);
1291 Py_END_CRITICAL_SECTION();
1292
1293 exit:
1294 return return_value;
1295 }
1296
1297 PyDoc_STRVAR(bytearray_strip__doc__,
1298 "strip($self, bytes=None, /)\n"
1299 "--\n"
1300 "\n"
1301 "Strip leading and trailing bytes contained in the argument.\n"
1302 "\n"
1303 "If the argument is omitted or None, strip leading and trailing ASCII\n"
1304 "whitespace.");
1305
1306 #define BYTEARRAY_STRIP_METHODDEF \
1307 {"strip", _PyCFunction_CAST(bytearray_strip), METH_FASTCALL, bytearray_strip__doc__},
1308
1309 static PyObject *
1310 bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
1311
1312 static PyObject *
1313 bytearray_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1314 {
1315 PyObject *return_value = NULL;
1316 PyObject *bytes = Py_None;
1317
1318 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
1319 goto exit;
1320 }
1321 if (nargs < 1) {
1322 goto skip_optional;
1323 }
1324 bytes = args[0];
1325 skip_optional:
1326 Py_BEGIN_CRITICAL_SECTION(self);
1327 return_value = bytearray_strip_impl((PyByteArrayObject *)self, bytes);
1328 Py_END_CRITICAL_SECTION();
1329
1330 exit:
1331 return return_value;
1332 }
1333
1334 PyDoc_STRVAR(bytearray_lstrip__doc__,
1335 "lstrip($self, bytes=None, /)\n"
1336 "--\n"
1337 "\n"
1338 "Strip leading bytes contained in the argument.\n"
1339 "\n"
1340 "If the argument is omitted or None, strip leading ASCII whitespace.");
1341
1342 #define BYTEARRAY_LSTRIP_METHODDEF \
1343 {"lstrip", _PyCFunction_CAST(bytearray_lstrip), METH_FASTCALL, bytearray_lstrip__doc__},
1344
1345 static PyObject *
1346 bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
1347
1348 static PyObject *
1349 bytearray_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1350 {
1351 PyObject *return_value = NULL;
1352 PyObject *bytes = Py_None;
1353
1354 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
1355 goto exit;
1356 }
1357 if (nargs < 1) {
1358 goto skip_optional;
1359 }
1360 bytes = args[0];
1361 skip_optional:
1362 Py_BEGIN_CRITICAL_SECTION(self);
1363 return_value = bytearray_lstrip_impl((PyByteArrayObject *)self, bytes);
1364 Py_END_CRITICAL_SECTION();
1365
1366 exit:
1367 return return_value;
1368 }
1369
1370 PyDoc_STRVAR(bytearray_rstrip__doc__,
1371 "rstrip($self, bytes=None, /)\n"
1372 "--\n"
1373 "\n"
1374 "Strip trailing bytes contained in the argument.\n"
1375 "\n"
1376 "If the argument is omitted or None, strip trailing ASCII whitespace.");
1377
1378 #define BYTEARRAY_RSTRIP_METHODDEF \
1379 {"rstrip", _PyCFunction_CAST(bytearray_rstrip), METH_FASTCALL, bytearray_rstrip__doc__},
1380
1381 static PyObject *
1382 bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
1383
1384 static PyObject *
1385 bytearray_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1386 {
1387 PyObject *return_value = NULL;
1388 PyObject *bytes = Py_None;
1389
1390 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
1391 goto exit;
1392 }
1393 if (nargs < 1) {
1394 goto skip_optional;
1395 }
1396 bytes = args[0];
1397 skip_optional:
1398 Py_BEGIN_CRITICAL_SECTION(self);
1399 return_value = bytearray_rstrip_impl((PyByteArrayObject *)self, bytes);
1400 Py_END_CRITICAL_SECTION();
1401
1402 exit:
1403 return return_value;
1404 }
1405
1406 PyDoc_STRVAR(bytearray_decode__doc__,
1407 "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
1408 "--\n"
1409 "\n"
1410 "Decode the bytearray using the codec registered for encoding.\n"
1411 "\n"
1412 " encoding\n"
1413 " The encoding with which to decode the bytearray.\n"
1414 " errors\n"
1415 " The error handling scheme to use for the handling of decoding\n"
1416 " errors. The default is \'strict\' meaning that decoding errors\n"
1417 " raise a UnicodeDecodeError. Other possible values are \'ignore\'\n"
1418 " and \'replace\' as well as any other name registered with\n"
1419 " codecs.register_error that can handle UnicodeDecodeErrors.");
1420
1421 #define BYTEARRAY_DECODE_METHODDEF \
1422 {"decode", _PyCFunction_CAST(bytearray_decode), METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
1423
1424 static PyObject *
1425 bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1426 const char *errors);
1427
1428 static PyObject *
1429 bytearray_decode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1430 {
1431 PyObject *return_value = NULL;
1432 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1433
1434 #define NUM_KEYWORDS 2
1435 static struct {
1436 PyGC_Head _this_is_not_used;
1437 PyObject_VAR_HEAD
1438 Py_hash_t ob_hash;
1439 PyObject *ob_item[NUM_KEYWORDS];
1440 } _kwtuple = {
1441 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1442 .ob_hash = -1,
1443 .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
1444 };
1445 #undef NUM_KEYWORDS
1446 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1447
1448 #else // !Py_BUILD_CORE
1449 # define KWTUPLE NULL
1450 #endif // !Py_BUILD_CORE
1451
1452 static const char * const _keywords[] = {"encoding", "errors", NULL};
1453 static _PyArg_Parser _parser = {
1454 .keywords = _keywords,
1455 .fname = "decode",
1456 .kwtuple = KWTUPLE,
1457 };
1458 #undef KWTUPLE
1459 PyObject *argsbuf[2];
1460 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1461 const char *encoding = NULL;
1462 const char *errors = NULL;
1463
1464 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1465 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1466 if (!args) {
1467 goto exit;
1468 }
1469 if (!noptargs) {
1470 goto skip_optional_pos;
1471 }
1472 if (args[0]) {
1473 if (!PyUnicode_Check(args[0])) {
1474 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
1475 goto exit;
1476 }
1477 Py_ssize_t encoding_length;
1478 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
1479 if (encoding == NULL) {
1480 goto exit;
1481 }
1482 if (strlen(encoding) != (size_t)encoding_length) {
1483 PyErr_SetString(PyExc_ValueError, "embedded null character");
1484 goto exit;
1485 }
1486 if (!--noptargs) {
1487 goto skip_optional_pos;
1488 }
1489 }
1490 if (!PyUnicode_Check(args[1])) {
1491 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
1492 goto exit;
1493 }
1494 Py_ssize_t errors_length;
1495 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1496 if (errors == NULL) {
1497 goto exit;
1498 }
1499 if (strlen(errors) != (size_t)errors_length) {
1500 PyErr_SetString(PyExc_ValueError, "embedded null character");
1501 goto exit;
1502 }
1503 skip_optional_pos:
1504 Py_BEGIN_CRITICAL_SECTION(self);
1505 return_value = bytearray_decode_impl((PyByteArrayObject *)self, encoding, errors);
1506 Py_END_CRITICAL_SECTION();
1507
1508 exit:
1509 return return_value;
1510 }
1511
1512 PyDoc_STRVAR(bytearray_join__doc__,
1513 "join($self, iterable_of_bytes, /)\n"
1514 "--\n"
1515 "\n"
1516 "Concatenate any number of bytes/bytearray objects.\n"
1517 "\n"
1518 "The bytearray whose method is called is inserted in between each\n"
1519 "pair.\n"
1520 "\n"
1521 "The result is returned as a new bytearray object.");
1522
1523 #define BYTEARRAY_JOIN_METHODDEF \
1524 {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
1525
1526 static PyObject *
1527 bytearray_join_impl(PyByteArrayObject *self, PyObject *iterable_of_bytes);
1528
1529 static PyObject *
1530 bytearray_join(PyObject *self, PyObject *iterable_of_bytes)
1531 {
1532 PyObject *return_value = NULL;
1533
1534 Py_BEGIN_CRITICAL_SECTION(self);
1535 return_value = bytearray_join_impl((PyByteArrayObject *)self, iterable_of_bytes);
1536 Py_END_CRITICAL_SECTION();
1537
1538 return return_value;
1539 }
1540
1541 PyDoc_STRVAR(bytearray_splitlines__doc__,
1542 "splitlines($self, /, keepends=False)\n"
1543 "--\n"
1544 "\n"
1545 "Return a list of the lines in the bytearray, breaking at line boundaries.\n"
1546 "\n"
1547 "Line breaks are not included in the resulting list unless keepends\n"
1548 "is given and true.");
1549
1550 #define BYTEARRAY_SPLITLINES_METHODDEF \
1551 {"splitlines", _PyCFunction_CAST(bytearray_splitlines), METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
1552
1553 static PyObject *
1554 bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
1555
1556 static PyObject *
1557 bytearray_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1558 {
1559 PyObject *return_value = NULL;
1560 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1561
1562 #define NUM_KEYWORDS 1
1563 static struct {
1564 PyGC_Head _this_is_not_used;
1565 PyObject_VAR_HEAD
1566 Py_hash_t ob_hash;
1567 PyObject *ob_item[NUM_KEYWORDS];
1568 } _kwtuple = {
1569 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1570 .ob_hash = -1,
1571 .ob_item = { &_Py_ID(keepends), },
1572 };
1573 #undef NUM_KEYWORDS
1574 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1575
1576 #else // !Py_BUILD_CORE
1577 # define KWTUPLE NULL
1578 #endif // !Py_BUILD_CORE
1579
1580 static const char * const _keywords[] = {"keepends", NULL};
1581 static _PyArg_Parser _parser = {
1582 .keywords = _keywords,
1583 .fname = "splitlines",
1584 .kwtuple = KWTUPLE,
1585 };
1586 #undef KWTUPLE
1587 PyObject *argsbuf[1];
1588 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1589 int keepends = 0;
1590
1591 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1592 /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1593 if (!args) {
1594 goto exit;
1595 }
1596 if (!noptargs) {
1597 goto skip_optional_pos;
1598 }
1599 keepends = PyObject_IsTrue(args[0]);
1600 if (keepends < 0) {
1601 goto exit;
1602 }
1603 skip_optional_pos:
1604 Py_BEGIN_CRITICAL_SECTION(self);
1605 return_value = bytearray_splitlines_impl((PyByteArrayObject *)self, keepends);
1606 Py_END_CRITICAL_SECTION();
1607
1608 exit:
1609 return return_value;
1610 }
1611
1612 PyDoc_STRVAR(bytearray_fromhex__doc__,
1613 "fromhex($type, string, /)\n"
1614 "--\n"
1615 "\n"
1616 "Create a bytearray object from a string of hexadecimal numbers.\n"
1617 "\n"
1618 "Spaces between two numbers are accepted.\n"
1619 "Example:\n"
1620 " bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
1621
1622 #define BYTEARRAY_FROMHEX_METHODDEF \
1623 {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
1624
1625 static PyObject *
1626 bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
1627
1628 static PyObject *
1629 bytearray_fromhex(PyObject *type, PyObject *string)
1630 {
1631 PyObject *return_value = NULL;
1632
1633 return_value = bytearray_fromhex_impl((PyTypeObject *)type, string);
1634
1635 return return_value;
1636 }
1637
1638 PyDoc_STRVAR(bytearray_hex__doc__,
1639 "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
1640 "--\n"
1641 "\n"
1642 "Create a string of hexadecimal numbers from a bytearray object.\n"
1643 "\n"
1644 " sep\n"
1645 " An optional single character or byte to separate hex bytes.\n"
1646 " bytes_per_sep\n"
1647 " How many bytes between separators. Positive values count from\n"
1648 " the right, negative values count from the left.\n"
1649 "\n"
1650 "Example:\n"
1651 ">>> value = bytearray([0xb9, 0x01, 0xef])\n"
1652 ">>> value.hex()\n"
1653 "\'b901ef\'\n"
1654 ">>> value.hex(\':\')\n"
1655 "\'b9:01:ef\'\n"
1656 ">>> value.hex(\':\', 2)\n"
1657 "\'b9:01ef\'\n"
1658 ">>> value.hex(\':\', -2)\n"
1659 "\'b901:ef\'");
1660
1661 #define BYTEARRAY_HEX_METHODDEF \
1662 {"hex", _PyCFunction_CAST(bytearray_hex), METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__},
1663
1664 static PyObject *
1665 bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep);
1666
1667 static PyObject *
1668 bytearray_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1669 {
1670 PyObject *return_value = NULL;
1671 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1672
1673 #define NUM_KEYWORDS 2
1674 static struct {
1675 PyGC_Head _this_is_not_used;
1676 PyObject_VAR_HEAD
1677 Py_hash_t ob_hash;
1678 PyObject *ob_item[NUM_KEYWORDS];
1679 } _kwtuple = {
1680 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1681 .ob_hash = -1,
1682 .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
1683 };
1684 #undef NUM_KEYWORDS
1685 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1686
1687 #else // !Py_BUILD_CORE
1688 # define KWTUPLE NULL
1689 #endif // !Py_BUILD_CORE
1690
1691 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
1692 static _PyArg_Parser _parser = {
1693 .keywords = _keywords,
1694 .fname = "hex",
1695 .kwtuple = KWTUPLE,
1696 };
1697 #undef KWTUPLE
1698 PyObject *argsbuf[2];
1699 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1700 PyObject *sep = NULL;
1701 int bytes_per_sep = 1;
1702
1703 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1704 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1705 if (!args) {
1706 goto exit;
1707 }
1708 if (!noptargs) {
1709 goto skip_optional_pos;
1710 }
1711 if (args[0]) {
1712 sep = args[0];
1713 if (!--noptargs) {
1714 goto skip_optional_pos;
1715 }
1716 }
1717 bytes_per_sep = PyLong_AsInt(args[1]);
1718 if (bytes_per_sep == -1 && PyErr_Occurred()) {
1719 goto exit;
1720 }
1721 skip_optional_pos:
1722 Py_BEGIN_CRITICAL_SECTION(self);
1723 return_value = bytearray_hex_impl((PyByteArrayObject *)self, sep, bytes_per_sep);
1724 Py_END_CRITICAL_SECTION();
1725
1726 exit:
1727 return return_value;
1728 }
1729
1730 PyDoc_STRVAR(bytearray_reduce__doc__,
1731 "__reduce__($self, /)\n"
1732 "--\n"
1733 "\n"
1734 "Return state information for pickling.");
1735
1736 #define BYTEARRAY_REDUCE_METHODDEF \
1737 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
1738
1739 static PyObject *
1740 bytearray_reduce_impl(PyByteArrayObject *self);
1741
1742 static PyObject *
1743 bytearray_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
1744 {
1745 PyObject *return_value = NULL;
1746
1747 Py_BEGIN_CRITICAL_SECTION(self);
1748 return_value = bytearray_reduce_impl((PyByteArrayObject *)self);
1749 Py_END_CRITICAL_SECTION();
1750
1751 return return_value;
1752 }
1753
1754 PyDoc_STRVAR(bytearray_reduce_ex__doc__,
1755 "__reduce_ex__($self, proto=0, /)\n"
1756 "--\n"
1757 "\n"
1758 "Return state information for pickling.");
1759
1760 #define BYTEARRAY_REDUCE_EX_METHODDEF \
1761 {"__reduce_ex__", _PyCFunction_CAST(bytearray_reduce_ex), METH_FASTCALL, bytearray_reduce_ex__doc__},
1762
1763 static PyObject *
1764 bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
1765
1766 static PyObject *
1767 bytearray_reduce_ex(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1768 {
1769 PyObject *return_value = NULL;
1770 int proto = 0;
1771
1772 if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
1773 goto exit;
1774 }
1775 if (nargs < 1) {
1776 goto skip_optional;
1777 }
1778 proto = PyLong_AsInt(args[0]);
1779 if (proto == -1 && PyErr_Occurred()) {
1780 goto exit;
1781 }
1782 skip_optional:
1783 Py_BEGIN_CRITICAL_SECTION(self);
1784 return_value = bytearray_reduce_ex_impl((PyByteArrayObject *)self, proto);
1785 Py_END_CRITICAL_SECTION();
1786
1787 exit:
1788 return return_value;
1789 }
1790
1791 PyDoc_STRVAR(bytearray_sizeof__doc__,
1792 "__sizeof__($self, /)\n"
1793 "--\n"
1794 "\n"
1795 "Returns the size of the bytearray object in memory, in bytes.");
1796
1797 #define BYTEARRAY_SIZEOF_METHODDEF \
1798 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
1799
1800 static PyObject *
1801 bytearray_sizeof_impl(PyByteArrayObject *self);
1802
1803 static PyObject *
1804 bytearray_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
1805 {
1806 return bytearray_sizeof_impl((PyByteArrayObject *)self);
1807 }
1808 /*[clinic end generated code: output=d13c01fc42a84ff5 input=a9049054013a1b77]*/