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 removed.\n"
646 "The remaining characters are mapped through the given translation table.");
647
648 #define BYTEARRAY_TRANSLATE_METHODDEF \
649 {"translate", _PyCFunction_CAST(bytearray_translate), METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__},
650
651 static PyObject *
652 bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
653 PyObject *deletechars);
654
655 static PyObject *
656 bytearray_translate(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
657 {
658 PyObject *return_value = NULL;
659 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
660
661 #define NUM_KEYWORDS 1
662 static struct {
663 PyGC_Head _this_is_not_used;
664 PyObject_VAR_HEAD
665 Py_hash_t ob_hash;
666 PyObject *ob_item[NUM_KEYWORDS];
667 } _kwtuple = {
668 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
669 .ob_hash = -1,
670 .ob_item = { &_Py_ID(delete), },
671 };
672 #undef NUM_KEYWORDS
673 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
674
675 #else // !Py_BUILD_CORE
676 # define KWTUPLE NULL
677 #endif // !Py_BUILD_CORE
678
679 static const char * const _keywords[] = {"", "delete", NULL};
680 static _PyArg_Parser _parser = {
681 .keywords = _keywords,
682 .fname = "translate",
683 .kwtuple = KWTUPLE,
684 };
685 #undef KWTUPLE
686 PyObject *argsbuf[2];
687 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
688 PyObject *table;
689 PyObject *deletechars = NULL;
690
691 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
692 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
693 if (!args) {
694 goto exit;
695 }
696 table = args[0];
697 if (!noptargs) {
698 goto skip_optional_pos;
699 }
700 deletechars = args[1];
701 skip_optional_pos:
702 Py_BEGIN_CRITICAL_SECTION(self);
703 return_value = bytearray_translate_impl((PyByteArrayObject *)self, table, deletechars);
704 Py_END_CRITICAL_SECTION();
705
706 exit:
707 return return_value;
708 }
709
710 PyDoc_STRVAR(bytearray_maketrans__doc__,
711 "maketrans(frm, to, /)\n"
712 "--\n"
713 "\n"
714 "Return a translation table usable for the bytes or bytearray translate method.\n"
715 "\n"
716 "The returned table will be one where each byte in frm is mapped to the byte at\n"
717 "the same position in to.\n"
718 "\n"
719 "The bytes objects frm and to must be of the same length.");
720
721 #define BYTEARRAY_MAKETRANS_METHODDEF \
722 {"maketrans", _PyCFunction_CAST(bytearray_maketrans), METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__},
723
724 static PyObject *
725 bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
726
727 static PyObject *
728 bytearray_maketrans(PyObject *null, PyObject *const *args, Py_ssize_t nargs)
729 {
730 PyObject *return_value = NULL;
731 Py_buffer frm = {NULL, NULL};
732 Py_buffer to = {NULL, NULL};
733
734 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
735 goto exit;
736 }
737 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
738 goto exit;
739 }
740 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
741 goto exit;
742 }
743 return_value = bytearray_maketrans_impl(&frm, &to);
744
745 exit:
746 /* Cleanup for frm */
747 if (frm.obj) {
748 PyBuffer_Release(&frm);
749 }
750 /* Cleanup for to */
751 if (to.obj) {
752 PyBuffer_Release(&to);
753 }
754
755 return return_value;
756 }
757
758 PyDoc_STRVAR(bytearray_replace__doc__,
759 "replace($self, old, new, count=-1, /)\n"
760 "--\n"
761 "\n"
762 "Return a copy with all occurrences of substring old replaced by new.\n"
763 "\n"
764 " count\n"
765 " Maximum number of occurrences to replace.\n"
766 " -1 (the default value) means replace all occurrences.\n"
767 "\n"
768 "If the optional argument count is given, only the first count occurrences are\n"
769 "replaced.");
770
771 #define BYTEARRAY_REPLACE_METHODDEF \
772 {"replace", _PyCFunction_CAST(bytearray_replace), METH_FASTCALL, bytearray_replace__doc__},
773
774 static PyObject *
775 bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
776 Py_buffer *new, Py_ssize_t count);
777
778 static PyObject *
779 bytearray_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
780 {
781 PyObject *return_value = NULL;
782 Py_buffer old = {NULL, NULL};
783 Py_buffer new = {NULL, NULL};
784 Py_ssize_t count = -1;
785
786 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
787 goto exit;
788 }
789 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
790 goto exit;
791 }
792 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
793 goto exit;
794 }
795 if (nargs < 3) {
796 goto skip_optional;
797 }
798 {
799 Py_ssize_t ival = -1;
800 PyObject *iobj = _PyNumber_Index(args[2]);
801 if (iobj != NULL) {
802 ival = PyLong_AsSsize_t(iobj);
803 Py_DECREF(iobj);
804 }
805 if (ival == -1 && PyErr_Occurred()) {
806 goto exit;
807 }
808 count = ival;
809 }
810 skip_optional:
811 Py_BEGIN_CRITICAL_SECTION(self);
812 return_value = bytearray_replace_impl((PyByteArrayObject *)self, &old, &new, count);
813 Py_END_CRITICAL_SECTION();
814
815 exit:
816 /* Cleanup for old */
817 if (old.obj) {
818 PyBuffer_Release(&old);
819 }
820 /* Cleanup for new */
821 if (new.obj) {
822 PyBuffer_Release(&new);
823 }
824
825 return return_value;
826 }
827
828 PyDoc_STRVAR(bytearray_split__doc__,
829 "split($self, /, sep=None, maxsplit=-1)\n"
830 "--\n"
831 "\n"
832 "Return a list of the sections in the bytearray, using sep as the delimiter.\n"
833 "\n"
834 " sep\n"
835 " The delimiter according which to split the bytearray.\n"
836 " None (the default value) means split on ASCII whitespace characters\n"
837 " (space, tab, return, newline, formfeed, vertical tab).\n"
838 " maxsplit\n"
839 " Maximum number of splits to do.\n"
840 " -1 (the default value) means no limit.");
841
842 #define BYTEARRAY_SPLIT_METHODDEF \
843 {"split", _PyCFunction_CAST(bytearray_split), METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
844
845 static PyObject *
846 bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
847 Py_ssize_t maxsplit);
848
849 static PyObject *
850 bytearray_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
851 {
852 PyObject *return_value = NULL;
853 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
854
855 #define NUM_KEYWORDS 2
856 static struct {
857 PyGC_Head _this_is_not_used;
858 PyObject_VAR_HEAD
859 Py_hash_t ob_hash;
860 PyObject *ob_item[NUM_KEYWORDS];
861 } _kwtuple = {
862 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
863 .ob_hash = -1,
864 .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
865 };
866 #undef NUM_KEYWORDS
867 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
868
869 #else // !Py_BUILD_CORE
870 # define KWTUPLE NULL
871 #endif // !Py_BUILD_CORE
872
873 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
874 static _PyArg_Parser _parser = {
875 .keywords = _keywords,
876 .fname = "split",
877 .kwtuple = KWTUPLE,
878 };
879 #undef KWTUPLE
880 PyObject *argsbuf[2];
881 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
882 PyObject *sep = Py_None;
883 Py_ssize_t maxsplit = -1;
884
885 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
886 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
887 if (!args) {
888 goto exit;
889 }
890 if (!noptargs) {
891 goto skip_optional_pos;
892 }
893 if (args[0]) {
894 sep = args[0];
895 if (!--noptargs) {
896 goto skip_optional_pos;
897 }
898 }
899 {
900 Py_ssize_t ival = -1;
901 PyObject *iobj = _PyNumber_Index(args[1]);
902 if (iobj != NULL) {
903 ival = PyLong_AsSsize_t(iobj);
904 Py_DECREF(iobj);
905 }
906 if (ival == -1 && PyErr_Occurred()) {
907 goto exit;
908 }
909 maxsplit = ival;
910 }
911 skip_optional_pos:
912 Py_BEGIN_CRITICAL_SECTION(self);
913 return_value = bytearray_split_impl((PyByteArrayObject *)self, sep, maxsplit);
914 Py_END_CRITICAL_SECTION();
915
916 exit:
917 return return_value;
918 }
919
920 PyDoc_STRVAR(bytearray_partition__doc__,
921 "partition($self, sep, /)\n"
922 "--\n"
923 "\n"
924 "Partition the bytearray into three parts using the given separator.\n"
925 "\n"
926 "This will search for the separator sep in the bytearray. If the separator is\n"
927 "found, returns a 3-tuple containing the part before the separator, the\n"
928 "separator itself, and the part after it as new bytearray objects.\n"
929 "\n"
930 "If the separator is not found, returns a 3-tuple containing the copy of the\n"
931 "original bytearray object and two empty bytearray objects.");
932
933 #define BYTEARRAY_PARTITION_METHODDEF \
934 {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
935
936 static PyObject *
937 bytearray_partition_impl(PyByteArrayObject *self, PyObject *sep);
938
939 static PyObject *
940 bytearray_partition(PyObject *self, PyObject *sep)
941 {
942 PyObject *return_value = NULL;
943
944 Py_BEGIN_CRITICAL_SECTION(self);
945 return_value = bytearray_partition_impl((PyByteArrayObject *)self, sep);
946 Py_END_CRITICAL_SECTION();
947
948 return return_value;
949 }
950
951 PyDoc_STRVAR(bytearray_rpartition__doc__,
952 "rpartition($self, sep, /)\n"
953 "--\n"
954 "\n"
955 "Partition the bytearray into three parts using the given separator.\n"
956 "\n"
957 "This will search for the separator sep in the bytearray, starting at the end.\n"
958 "If the separator is found, returns a 3-tuple containing the part before the\n"
959 "separator, the separator itself, and the part after it as new bytearray\n"
960 "objects.\n"
961 "\n"
962 "If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
963 "objects and the copy of the original bytearray object.");
964
965 #define BYTEARRAY_RPARTITION_METHODDEF \
966 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
967
968 static PyObject *
969 bytearray_rpartition_impl(PyByteArrayObject *self, PyObject *sep);
970
971 static PyObject *
972 bytearray_rpartition(PyObject *self, PyObject *sep)
973 {
974 PyObject *return_value = NULL;
975
976 Py_BEGIN_CRITICAL_SECTION(self);
977 return_value = bytearray_rpartition_impl((PyByteArrayObject *)self, sep);
978 Py_END_CRITICAL_SECTION();
979
980 return return_value;
981 }
982
983 PyDoc_STRVAR(bytearray_rsplit__doc__,
984 "rsplit($self, /, sep=None, maxsplit=-1)\n"
985 "--\n"
986 "\n"
987 "Return a list of the sections in the bytearray, using sep as the delimiter.\n"
988 "\n"
989 " sep\n"
990 " The delimiter according which to split the bytearray.\n"
991 " None (the default value) means split on ASCII whitespace characters\n"
992 " (space, tab, return, newline, formfeed, vertical tab).\n"
993 " maxsplit\n"
994 " Maximum number of splits to do.\n"
995 " -1 (the default value) means no limit.\n"
996 "\n"
997 "Splitting is done starting at the end of the bytearray and working to the front.");
998
999 #define BYTEARRAY_RSPLIT_METHODDEF \
1000 {"rsplit", _PyCFunction_CAST(bytearray_rsplit), METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
1001
1002 static PyObject *
1003 bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1004 Py_ssize_t maxsplit);
1005
1006 static PyObject *
1007 bytearray_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1008 {
1009 PyObject *return_value = NULL;
1010 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1011
1012 #define NUM_KEYWORDS 2
1013 static struct {
1014 PyGC_Head _this_is_not_used;
1015 PyObject_VAR_HEAD
1016 Py_hash_t ob_hash;
1017 PyObject *ob_item[NUM_KEYWORDS];
1018 } _kwtuple = {
1019 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1020 .ob_hash = -1,
1021 .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), },
1022 };
1023 #undef NUM_KEYWORDS
1024 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1025
1026 #else // !Py_BUILD_CORE
1027 # define KWTUPLE NULL
1028 #endif // !Py_BUILD_CORE
1029
1030 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
1031 static _PyArg_Parser _parser = {
1032 .keywords = _keywords,
1033 .fname = "rsplit",
1034 .kwtuple = KWTUPLE,
1035 };
1036 #undef KWTUPLE
1037 PyObject *argsbuf[2];
1038 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1039 PyObject *sep = Py_None;
1040 Py_ssize_t maxsplit = -1;
1041
1042 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1043 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1044 if (!args) {
1045 goto exit;
1046 }
1047 if (!noptargs) {
1048 goto skip_optional_pos;
1049 }
1050 if (args[0]) {
1051 sep = args[0];
1052 if (!--noptargs) {
1053 goto skip_optional_pos;
1054 }
1055 }
1056 {
1057 Py_ssize_t ival = -1;
1058 PyObject *iobj = _PyNumber_Index(args[1]);
1059 if (iobj != NULL) {
1060 ival = PyLong_AsSsize_t(iobj);
1061 Py_DECREF(iobj);
1062 }
1063 if (ival == -1 && PyErr_Occurred()) {
1064 goto exit;
1065 }
1066 maxsplit = ival;
1067 }
1068 skip_optional_pos:
1069 Py_BEGIN_CRITICAL_SECTION(self);
1070 return_value = bytearray_rsplit_impl((PyByteArrayObject *)self, sep, maxsplit);
1071 Py_END_CRITICAL_SECTION();
1072
1073 exit:
1074 return return_value;
1075 }
1076
1077 PyDoc_STRVAR(bytearray_reverse__doc__,
1078 "reverse($self, /)\n"
1079 "--\n"
1080 "\n"
1081 "Reverse the order of the values in B in place.");
1082
1083 #define BYTEARRAY_REVERSE_METHODDEF \
1084 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
1085
1086 static PyObject *
1087 bytearray_reverse_impl(PyByteArrayObject *self);
1088
1089 static PyObject *
1090 bytearray_reverse(PyObject *self, PyObject *Py_UNUSED(ignored))
1091 {
1092 PyObject *return_value = NULL;
1093
1094 Py_BEGIN_CRITICAL_SECTION(self);
1095 return_value = bytearray_reverse_impl((PyByteArrayObject *)self);
1096 Py_END_CRITICAL_SECTION();
1097
1098 return return_value;
1099 }
1100
1101 PyDoc_STRVAR(bytearray_insert__doc__,
1102 "insert($self, index, item, /)\n"
1103 "--\n"
1104 "\n"
1105 "Insert a single item into the bytearray before the given index.\n"
1106 "\n"
1107 " index\n"
1108 " The index where the value is to be inserted.\n"
1109 " item\n"
1110 " The item to be inserted.");
1111
1112 #define BYTEARRAY_INSERT_METHODDEF \
1113 {"insert", _PyCFunction_CAST(bytearray_insert), METH_FASTCALL, bytearray_insert__doc__},
1114
1115 static PyObject *
1116 bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
1117
1118 static PyObject *
1119 bytearray_insert(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1120 {
1121 PyObject *return_value = NULL;
1122 Py_ssize_t index;
1123 int item;
1124
1125 if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
1126 goto exit;
1127 }
1128 {
1129 Py_ssize_t ival = -1;
1130 PyObject *iobj = _PyNumber_Index(args[0]);
1131 if (iobj != NULL) {
1132 ival = PyLong_AsSsize_t(iobj);
1133 Py_DECREF(iobj);
1134 }
1135 if (ival == -1 && PyErr_Occurred()) {
1136 goto exit;
1137 }
1138 index = ival;
1139 }
1140 if (!_getbytevalue(args[1], &item)) {
1141 goto exit;
1142 }
1143 Py_BEGIN_CRITICAL_SECTION(self);
1144 return_value = bytearray_insert_impl((PyByteArrayObject *)self, index, item);
1145 Py_END_CRITICAL_SECTION();
1146
1147 exit:
1148 return return_value;
1149 }
1150
1151 PyDoc_STRVAR(bytearray_append__doc__,
1152 "append($self, item, /)\n"
1153 "--\n"
1154 "\n"
1155 "Append a single item to the end of the bytearray.\n"
1156 "\n"
1157 " item\n"
1158 " The item to be appended.");
1159
1160 #define BYTEARRAY_APPEND_METHODDEF \
1161 {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
1162
1163 static PyObject *
1164 bytearray_append_impl(PyByteArrayObject *self, int item);
1165
1166 static PyObject *
1167 bytearray_append(PyObject *self, PyObject *arg)
1168 {
1169 PyObject *return_value = NULL;
1170 int item;
1171
1172 if (!_getbytevalue(arg, &item)) {
1173 goto exit;
1174 }
1175 Py_BEGIN_CRITICAL_SECTION(self);
1176 return_value = bytearray_append_impl((PyByteArrayObject *)self, item);
1177 Py_END_CRITICAL_SECTION();
1178
1179 exit:
1180 return return_value;
1181 }
1182
1183 PyDoc_STRVAR(bytearray_extend__doc__,
1184 "extend($self, iterable_of_ints, /)\n"
1185 "--\n"
1186 "\n"
1187 "Append all the items from the iterator or sequence to the end of the bytearray.\n"
1188 "\n"
1189 " iterable_of_ints\n"
1190 " The iterable of items to append.");
1191
1192 #define BYTEARRAY_EXTEND_METHODDEF \
1193 {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
1194
1195 static PyObject *
1196 bytearray_extend_impl(PyByteArrayObject *self, PyObject *iterable_of_ints);
1197
1198 static PyObject *
1199 bytearray_extend(PyObject *self, PyObject *iterable_of_ints)
1200 {
1201 PyObject *return_value = NULL;
1202
1203 Py_BEGIN_CRITICAL_SECTION(self);
1204 return_value = bytearray_extend_impl((PyByteArrayObject *)self, iterable_of_ints);
1205 Py_END_CRITICAL_SECTION();
1206
1207 return return_value;
1208 }
1209
1210 PyDoc_STRVAR(bytearray_pop__doc__,
1211 "pop($self, index=-1, /)\n"
1212 "--\n"
1213 "\n"
1214 "Remove and return a single item from B.\n"
1215 "\n"
1216 " index\n"
1217 " The index from where to remove the item.\n"
1218 " -1 (the default value) means remove the last item.\n"
1219 "\n"
1220 "If no index argument is given, will pop the last item.");
1221
1222 #define BYTEARRAY_POP_METHODDEF \
1223 {"pop", _PyCFunction_CAST(bytearray_pop), METH_FASTCALL, bytearray_pop__doc__},
1224
1225 static PyObject *
1226 bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
1227
1228 static PyObject *
1229 bytearray_pop(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1230 {
1231 PyObject *return_value = NULL;
1232 Py_ssize_t index = -1;
1233
1234 if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
1235 goto exit;
1236 }
1237 if (nargs < 1) {
1238 goto skip_optional;
1239 }
1240 {
1241 Py_ssize_t ival = -1;
1242 PyObject *iobj = _PyNumber_Index(args[0]);
1243 if (iobj != NULL) {
1244 ival = PyLong_AsSsize_t(iobj);
1245 Py_DECREF(iobj);
1246 }
1247 if (ival == -1 && PyErr_Occurred()) {
1248 goto exit;
1249 }
1250 index = ival;
1251 }
1252 skip_optional:
1253 Py_BEGIN_CRITICAL_SECTION(self);
1254 return_value = bytearray_pop_impl((PyByteArrayObject *)self, index);
1255 Py_END_CRITICAL_SECTION();
1256
1257 exit:
1258 return return_value;
1259 }
1260
1261 PyDoc_STRVAR(bytearray_remove__doc__,
1262 "remove($self, value, /)\n"
1263 "--\n"
1264 "\n"
1265 "Remove the first occurrence of a value in the bytearray.\n"
1266 "\n"
1267 " value\n"
1268 " The value to remove.");
1269
1270 #define BYTEARRAY_REMOVE_METHODDEF \
1271 {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
1272
1273 static PyObject *
1274 bytearray_remove_impl(PyByteArrayObject *self, int value);
1275
1276 static PyObject *
1277 bytearray_remove(PyObject *self, PyObject *arg)
1278 {
1279 PyObject *return_value = NULL;
1280 int value;
1281
1282 if (!_getbytevalue(arg, &value)) {
1283 goto exit;
1284 }
1285 Py_BEGIN_CRITICAL_SECTION(self);
1286 return_value = bytearray_remove_impl((PyByteArrayObject *)self, value);
1287 Py_END_CRITICAL_SECTION();
1288
1289 exit:
1290 return return_value;
1291 }
1292
1293 PyDoc_STRVAR(bytearray_strip__doc__,
1294 "strip($self, bytes=None, /)\n"
1295 "--\n"
1296 "\n"
1297 "Strip leading and trailing bytes contained in the argument.\n"
1298 "\n"
1299 "If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
1300
1301 #define BYTEARRAY_STRIP_METHODDEF \
1302 {"strip", _PyCFunction_CAST(bytearray_strip), METH_FASTCALL, bytearray_strip__doc__},
1303
1304 static PyObject *
1305 bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
1306
1307 static PyObject *
1308 bytearray_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1309 {
1310 PyObject *return_value = NULL;
1311 PyObject *bytes = Py_None;
1312
1313 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
1314 goto exit;
1315 }
1316 if (nargs < 1) {
1317 goto skip_optional;
1318 }
1319 bytes = args[0];
1320 skip_optional:
1321 Py_BEGIN_CRITICAL_SECTION(self);
1322 return_value = bytearray_strip_impl((PyByteArrayObject *)self, bytes);
1323 Py_END_CRITICAL_SECTION();
1324
1325 exit:
1326 return return_value;
1327 }
1328
1329 PyDoc_STRVAR(bytearray_lstrip__doc__,
1330 "lstrip($self, bytes=None, /)\n"
1331 "--\n"
1332 "\n"
1333 "Strip leading bytes contained in the argument.\n"
1334 "\n"
1335 "If the argument is omitted or None, strip leading ASCII whitespace.");
1336
1337 #define BYTEARRAY_LSTRIP_METHODDEF \
1338 {"lstrip", _PyCFunction_CAST(bytearray_lstrip), METH_FASTCALL, bytearray_lstrip__doc__},
1339
1340 static PyObject *
1341 bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
1342
1343 static PyObject *
1344 bytearray_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1345 {
1346 PyObject *return_value = NULL;
1347 PyObject *bytes = Py_None;
1348
1349 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
1350 goto exit;
1351 }
1352 if (nargs < 1) {
1353 goto skip_optional;
1354 }
1355 bytes = args[0];
1356 skip_optional:
1357 Py_BEGIN_CRITICAL_SECTION(self);
1358 return_value = bytearray_lstrip_impl((PyByteArrayObject *)self, bytes);
1359 Py_END_CRITICAL_SECTION();
1360
1361 exit:
1362 return return_value;
1363 }
1364
1365 PyDoc_STRVAR(bytearray_rstrip__doc__,
1366 "rstrip($self, bytes=None, /)\n"
1367 "--\n"
1368 "\n"
1369 "Strip trailing bytes contained in the argument.\n"
1370 "\n"
1371 "If the argument is omitted or None, strip trailing ASCII whitespace.");
1372
1373 #define BYTEARRAY_RSTRIP_METHODDEF \
1374 {"rstrip", _PyCFunction_CAST(bytearray_rstrip), METH_FASTCALL, bytearray_rstrip__doc__},
1375
1376 static PyObject *
1377 bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
1378
1379 static PyObject *
1380 bytearray_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1381 {
1382 PyObject *return_value = NULL;
1383 PyObject *bytes = Py_None;
1384
1385 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
1386 goto exit;
1387 }
1388 if (nargs < 1) {
1389 goto skip_optional;
1390 }
1391 bytes = args[0];
1392 skip_optional:
1393 Py_BEGIN_CRITICAL_SECTION(self);
1394 return_value = bytearray_rstrip_impl((PyByteArrayObject *)self, bytes);
1395 Py_END_CRITICAL_SECTION();
1396
1397 exit:
1398 return return_value;
1399 }
1400
1401 PyDoc_STRVAR(bytearray_decode__doc__,
1402 "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
1403 "--\n"
1404 "\n"
1405 "Decode the bytearray using the codec registered for encoding.\n"
1406 "\n"
1407 " encoding\n"
1408 " The encoding with which to decode the bytearray.\n"
1409 " errors\n"
1410 " The error handling scheme to use for the handling of decoding errors.\n"
1411 " The default is \'strict\' meaning that decoding errors raise a\n"
1412 " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
1413 " as well as any other name registered with codecs.register_error that\n"
1414 " can handle UnicodeDecodeErrors.");
1415
1416 #define BYTEARRAY_DECODE_METHODDEF \
1417 {"decode", _PyCFunction_CAST(bytearray_decode), METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
1418
1419 static PyObject *
1420 bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1421 const char *errors);
1422
1423 static PyObject *
1424 bytearray_decode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1425 {
1426 PyObject *return_value = NULL;
1427 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1428
1429 #define NUM_KEYWORDS 2
1430 static struct {
1431 PyGC_Head _this_is_not_used;
1432 PyObject_VAR_HEAD
1433 Py_hash_t ob_hash;
1434 PyObject *ob_item[NUM_KEYWORDS];
1435 } _kwtuple = {
1436 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1437 .ob_hash = -1,
1438 .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), },
1439 };
1440 #undef NUM_KEYWORDS
1441 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1442
1443 #else // !Py_BUILD_CORE
1444 # define KWTUPLE NULL
1445 #endif // !Py_BUILD_CORE
1446
1447 static const char * const _keywords[] = {"encoding", "errors", NULL};
1448 static _PyArg_Parser _parser = {
1449 .keywords = _keywords,
1450 .fname = "decode",
1451 .kwtuple = KWTUPLE,
1452 };
1453 #undef KWTUPLE
1454 PyObject *argsbuf[2];
1455 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1456 const char *encoding = NULL;
1457 const char *errors = NULL;
1458
1459 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1460 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1461 if (!args) {
1462 goto exit;
1463 }
1464 if (!noptargs) {
1465 goto skip_optional_pos;
1466 }
1467 if (args[0]) {
1468 if (!PyUnicode_Check(args[0])) {
1469 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
1470 goto exit;
1471 }
1472 Py_ssize_t encoding_length;
1473 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
1474 if (encoding == NULL) {
1475 goto exit;
1476 }
1477 if (strlen(encoding) != (size_t)encoding_length) {
1478 PyErr_SetString(PyExc_ValueError, "embedded null character");
1479 goto exit;
1480 }
1481 if (!--noptargs) {
1482 goto skip_optional_pos;
1483 }
1484 }
1485 if (!PyUnicode_Check(args[1])) {
1486 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
1487 goto exit;
1488 }
1489 Py_ssize_t errors_length;
1490 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1491 if (errors == NULL) {
1492 goto exit;
1493 }
1494 if (strlen(errors) != (size_t)errors_length) {
1495 PyErr_SetString(PyExc_ValueError, "embedded null character");
1496 goto exit;
1497 }
1498 skip_optional_pos:
1499 Py_BEGIN_CRITICAL_SECTION(self);
1500 return_value = bytearray_decode_impl((PyByteArrayObject *)self, encoding, errors);
1501 Py_END_CRITICAL_SECTION();
1502
1503 exit:
1504 return return_value;
1505 }
1506
1507 PyDoc_STRVAR(bytearray_join__doc__,
1508 "join($self, iterable_of_bytes, /)\n"
1509 "--\n"
1510 "\n"
1511 "Concatenate any number of bytes/bytearray objects.\n"
1512 "\n"
1513 "The bytearray whose method is called is inserted in between each pair.\n"
1514 "\n"
1515 "The result is returned as a new bytearray object.");
1516
1517 #define BYTEARRAY_JOIN_METHODDEF \
1518 {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
1519
1520 static PyObject *
1521 bytearray_join_impl(PyByteArrayObject *self, PyObject *iterable_of_bytes);
1522
1523 static PyObject *
1524 bytearray_join(PyObject *self, PyObject *iterable_of_bytes)
1525 {
1526 PyObject *return_value = NULL;
1527
1528 Py_BEGIN_CRITICAL_SECTION(self);
1529 return_value = bytearray_join_impl((PyByteArrayObject *)self, iterable_of_bytes);
1530 Py_END_CRITICAL_SECTION();
1531
1532 return return_value;
1533 }
1534
1535 PyDoc_STRVAR(bytearray_splitlines__doc__,
1536 "splitlines($self, /, keepends=False)\n"
1537 "--\n"
1538 "\n"
1539 "Return a list of the lines in the bytearray, breaking at line boundaries.\n"
1540 "\n"
1541 "Line breaks are not included in the resulting list unless keepends is given and\n"
1542 "true.");
1543
1544 #define BYTEARRAY_SPLITLINES_METHODDEF \
1545 {"splitlines", _PyCFunction_CAST(bytearray_splitlines), METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
1546
1547 static PyObject *
1548 bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
1549
1550 static PyObject *
1551 bytearray_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1552 {
1553 PyObject *return_value = NULL;
1554 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1555
1556 #define NUM_KEYWORDS 1
1557 static struct {
1558 PyGC_Head _this_is_not_used;
1559 PyObject_VAR_HEAD
1560 Py_hash_t ob_hash;
1561 PyObject *ob_item[NUM_KEYWORDS];
1562 } _kwtuple = {
1563 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1564 .ob_hash = -1,
1565 .ob_item = { &_Py_ID(keepends), },
1566 };
1567 #undef NUM_KEYWORDS
1568 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1569
1570 #else // !Py_BUILD_CORE
1571 # define KWTUPLE NULL
1572 #endif // !Py_BUILD_CORE
1573
1574 static const char * const _keywords[] = {"keepends", NULL};
1575 static _PyArg_Parser _parser = {
1576 .keywords = _keywords,
1577 .fname = "splitlines",
1578 .kwtuple = KWTUPLE,
1579 };
1580 #undef KWTUPLE
1581 PyObject *argsbuf[1];
1582 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1583 int keepends = 0;
1584
1585 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1586 /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1587 if (!args) {
1588 goto exit;
1589 }
1590 if (!noptargs) {
1591 goto skip_optional_pos;
1592 }
1593 keepends = PyObject_IsTrue(args[0]);
1594 if (keepends < 0) {
1595 goto exit;
1596 }
1597 skip_optional_pos:
1598 Py_BEGIN_CRITICAL_SECTION(self);
1599 return_value = bytearray_splitlines_impl((PyByteArrayObject *)self, keepends);
1600 Py_END_CRITICAL_SECTION();
1601
1602 exit:
1603 return return_value;
1604 }
1605
1606 PyDoc_STRVAR(bytearray_fromhex__doc__,
1607 "fromhex($type, string, /)\n"
1608 "--\n"
1609 "\n"
1610 "Create a bytearray object from a string of hexadecimal numbers.\n"
1611 "\n"
1612 "Spaces between two numbers are accepted.\n"
1613 "Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
1614
1615 #define BYTEARRAY_FROMHEX_METHODDEF \
1616 {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
1617
1618 static PyObject *
1619 bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
1620
1621 static PyObject *
1622 bytearray_fromhex(PyObject *type, PyObject *string)
1623 {
1624 PyObject *return_value = NULL;
1625
1626 return_value = bytearray_fromhex_impl((PyTypeObject *)type, string);
1627
1628 return return_value;
1629 }
1630
1631 PyDoc_STRVAR(bytearray_hex__doc__,
1632 "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
1633 "--\n"
1634 "\n"
1635 "Create a string of hexadecimal numbers from a bytearray object.\n"
1636 "\n"
1637 " sep\n"
1638 " An optional single character or byte to separate hex bytes.\n"
1639 " bytes_per_sep\n"
1640 " How many bytes between separators. Positive values count from the\n"
1641 " right, negative values count from the left.\n"
1642 "\n"
1643 "Example:\n"
1644 ">>> value = bytearray([0xb9, 0x01, 0xef])\n"
1645 ">>> value.hex()\n"
1646 "\'b901ef\'\n"
1647 ">>> value.hex(\':\')\n"
1648 "\'b9:01:ef\'\n"
1649 ">>> value.hex(\':\', 2)\n"
1650 "\'b9:01ef\'\n"
1651 ">>> value.hex(\':\', -2)\n"
1652 "\'b901:ef\'");
1653
1654 #define BYTEARRAY_HEX_METHODDEF \
1655 {"hex", _PyCFunction_CAST(bytearray_hex), METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__},
1656
1657 static PyObject *
1658 bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep);
1659
1660 static PyObject *
1661 bytearray_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1662 {
1663 PyObject *return_value = NULL;
1664 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1665
1666 #define NUM_KEYWORDS 2
1667 static struct {
1668 PyGC_Head _this_is_not_used;
1669 PyObject_VAR_HEAD
1670 Py_hash_t ob_hash;
1671 PyObject *ob_item[NUM_KEYWORDS];
1672 } _kwtuple = {
1673 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1674 .ob_hash = -1,
1675 .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
1676 };
1677 #undef NUM_KEYWORDS
1678 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1679
1680 #else // !Py_BUILD_CORE
1681 # define KWTUPLE NULL
1682 #endif // !Py_BUILD_CORE
1683
1684 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
1685 static _PyArg_Parser _parser = {
1686 .keywords = _keywords,
1687 .fname = "hex",
1688 .kwtuple = KWTUPLE,
1689 };
1690 #undef KWTUPLE
1691 PyObject *argsbuf[2];
1692 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1693 PyObject *sep = NULL;
1694 int bytes_per_sep = 1;
1695
1696 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1697 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1698 if (!args) {
1699 goto exit;
1700 }
1701 if (!noptargs) {
1702 goto skip_optional_pos;
1703 }
1704 if (args[0]) {
1705 sep = args[0];
1706 if (!--noptargs) {
1707 goto skip_optional_pos;
1708 }
1709 }
1710 bytes_per_sep = PyLong_AsInt(args[1]);
1711 if (bytes_per_sep == -1 && PyErr_Occurred()) {
1712 goto exit;
1713 }
1714 skip_optional_pos:
1715 Py_BEGIN_CRITICAL_SECTION(self);
1716 return_value = bytearray_hex_impl((PyByteArrayObject *)self, sep, bytes_per_sep);
1717 Py_END_CRITICAL_SECTION();
1718
1719 exit:
1720 return return_value;
1721 }
1722
1723 PyDoc_STRVAR(bytearray_reduce__doc__,
1724 "__reduce__($self, /)\n"
1725 "--\n"
1726 "\n"
1727 "Return state information for pickling.");
1728
1729 #define BYTEARRAY_REDUCE_METHODDEF \
1730 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
1731
1732 static PyObject *
1733 bytearray_reduce_impl(PyByteArrayObject *self);
1734
1735 static PyObject *
1736 bytearray_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
1737 {
1738 PyObject *return_value = NULL;
1739
1740 Py_BEGIN_CRITICAL_SECTION(self);
1741 return_value = bytearray_reduce_impl((PyByteArrayObject *)self);
1742 Py_END_CRITICAL_SECTION();
1743
1744 return return_value;
1745 }
1746
1747 PyDoc_STRVAR(bytearray_reduce_ex__doc__,
1748 "__reduce_ex__($self, proto=0, /)\n"
1749 "--\n"
1750 "\n"
1751 "Return state information for pickling.");
1752
1753 #define BYTEARRAY_REDUCE_EX_METHODDEF \
1754 {"__reduce_ex__", _PyCFunction_CAST(bytearray_reduce_ex), METH_FASTCALL, bytearray_reduce_ex__doc__},
1755
1756 static PyObject *
1757 bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
1758
1759 static PyObject *
1760 bytearray_reduce_ex(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
1761 {
1762 PyObject *return_value = NULL;
1763 int proto = 0;
1764
1765 if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
1766 goto exit;
1767 }
1768 if (nargs < 1) {
1769 goto skip_optional;
1770 }
1771 proto = PyLong_AsInt(args[0]);
1772 if (proto == -1 && PyErr_Occurred()) {
1773 goto exit;
1774 }
1775 skip_optional:
1776 Py_BEGIN_CRITICAL_SECTION(self);
1777 return_value = bytearray_reduce_ex_impl((PyByteArrayObject *)self, proto);
1778 Py_END_CRITICAL_SECTION();
1779
1780 exit:
1781 return return_value;
1782 }
1783
1784 PyDoc_STRVAR(bytearray_sizeof__doc__,
1785 "__sizeof__($self, /)\n"
1786 "--\n"
1787 "\n"
1788 "Returns the size of the bytearray object in memory, in bytes.");
1789
1790 #define BYTEARRAY_SIZEOF_METHODDEF \
1791 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
1792
1793 static PyObject *
1794 bytearray_sizeof_impl(PyByteArrayObject *self);
1795
1796 static PyObject *
1797 bytearray_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored))
1798 {
1799 return bytearray_sizeof_impl((PyByteArrayObject *)self);
1800 }
1801 /*[clinic end generated code: output=bca62cf335d48127 input=a9049054013a1b77]*/