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