1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 static int
6 bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
7 const char *encoding, const char *errors);
8
9 static int
bytearray___init__(PyObject * self,PyObject * args,PyObject * kwargs)10 bytearray___init__(PyObject *self, PyObject *args, PyObject *kwargs)
11 {
12 int return_value = -1;
13 static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
14 static _PyArg_Parser _parser = {NULL, _keywords, "bytearray", 0};
15 PyObject *argsbuf[3];
16 PyObject * const *fastargs;
17 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
18 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
19 PyObject *arg = NULL;
20 const char *encoding = NULL;
21 const char *errors = NULL;
22
23 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
24 if (!fastargs) {
25 goto exit;
26 }
27 if (!noptargs) {
28 goto skip_optional_pos;
29 }
30 if (fastargs[0]) {
31 arg = fastargs[0];
32 if (!--noptargs) {
33 goto skip_optional_pos;
34 }
35 }
36 if (fastargs[1]) {
37 if (!PyUnicode_Check(fastargs[1])) {
38 _PyArg_BadArgument("bytearray", "argument 'encoding'", "str", fastargs[1]);
39 goto exit;
40 }
41 Py_ssize_t encoding_length;
42 encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
43 if (encoding == NULL) {
44 goto exit;
45 }
46 if (strlen(encoding) != (size_t)encoding_length) {
47 PyErr_SetString(PyExc_ValueError, "embedded null character");
48 goto exit;
49 }
50 if (!--noptargs) {
51 goto skip_optional_pos;
52 }
53 }
54 if (!PyUnicode_Check(fastargs[2])) {
55 _PyArg_BadArgument("bytearray", "argument 'errors'", "str", fastargs[2]);
56 goto exit;
57 }
58 Py_ssize_t errors_length;
59 errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
60 if (errors == NULL) {
61 goto exit;
62 }
63 if (strlen(errors) != (size_t)errors_length) {
64 PyErr_SetString(PyExc_ValueError, "embedded null character");
65 goto exit;
66 }
67 skip_optional_pos:
68 return_value = bytearray___init___impl((PyByteArrayObject *)self, arg, encoding, errors);
69
70 exit:
71 return return_value;
72 }
73
74 PyDoc_STRVAR(bytearray_clear__doc__,
75 "clear($self, /)\n"
76 "--\n"
77 "\n"
78 "Remove all items from the bytearray.");
79
80 #define BYTEARRAY_CLEAR_METHODDEF \
81 {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, bytearray_clear__doc__},
82
83 static PyObject *
84 bytearray_clear_impl(PyByteArrayObject *self);
85
86 static PyObject *
bytearray_clear(PyByteArrayObject * self,PyObject * Py_UNUSED (ignored))87 bytearray_clear(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
88 {
89 return bytearray_clear_impl(self);
90 }
91
92 PyDoc_STRVAR(bytearray_copy__doc__,
93 "copy($self, /)\n"
94 "--\n"
95 "\n"
96 "Return a copy of B.");
97
98 #define BYTEARRAY_COPY_METHODDEF \
99 {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, bytearray_copy__doc__},
100
101 static PyObject *
102 bytearray_copy_impl(PyByteArrayObject *self);
103
104 static PyObject *
bytearray_copy(PyByteArrayObject * self,PyObject * Py_UNUSED (ignored))105 bytearray_copy(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
106 {
107 return bytearray_copy_impl(self);
108 }
109
110 PyDoc_STRVAR(bytearray_removeprefix__doc__,
111 "removeprefix($self, prefix, /)\n"
112 "--\n"
113 "\n"
114 "Return a bytearray with the given prefix string removed if present.\n"
115 "\n"
116 "If the bytearray starts with the prefix string, return\n"
117 "bytearray[len(prefix):]. Otherwise, return a copy of the original\n"
118 "bytearray.");
119
120 #define BYTEARRAY_REMOVEPREFIX_METHODDEF \
121 {"removeprefix", (PyCFunction)bytearray_removeprefix, METH_O, bytearray_removeprefix__doc__},
122
123 static PyObject *
124 bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix);
125
126 static PyObject *
bytearray_removeprefix(PyByteArrayObject * self,PyObject * arg)127 bytearray_removeprefix(PyByteArrayObject *self, PyObject *arg)
128 {
129 PyObject *return_value = NULL;
130 Py_buffer prefix = {NULL, NULL};
131
132 if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
133 goto exit;
134 }
135 if (!PyBuffer_IsContiguous(&prefix, 'C')) {
136 _PyArg_BadArgument("removeprefix", "argument", "contiguous buffer", arg);
137 goto exit;
138 }
139 return_value = bytearray_removeprefix_impl(self, &prefix);
140
141 exit:
142 /* Cleanup for prefix */
143 if (prefix.obj) {
144 PyBuffer_Release(&prefix);
145 }
146
147 return return_value;
148 }
149
150 PyDoc_STRVAR(bytearray_removesuffix__doc__,
151 "removesuffix($self, suffix, /)\n"
152 "--\n"
153 "\n"
154 "Return a bytearray with the given suffix string removed if present.\n"
155 "\n"
156 "If the bytearray ends with the suffix string and that suffix is not\n"
157 "empty, return bytearray[:-len(suffix)]. Otherwise, return a copy of\n"
158 "the original bytearray.");
159
160 #define BYTEARRAY_REMOVESUFFIX_METHODDEF \
161 {"removesuffix", (PyCFunction)bytearray_removesuffix, METH_O, bytearray_removesuffix__doc__},
162
163 static PyObject *
164 bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix);
165
166 static PyObject *
bytearray_removesuffix(PyByteArrayObject * self,PyObject * arg)167 bytearray_removesuffix(PyByteArrayObject *self, PyObject *arg)
168 {
169 PyObject *return_value = NULL;
170 Py_buffer suffix = {NULL, NULL};
171
172 if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
173 goto exit;
174 }
175 if (!PyBuffer_IsContiguous(&suffix, 'C')) {
176 _PyArg_BadArgument("removesuffix", "argument", "contiguous buffer", arg);
177 goto exit;
178 }
179 return_value = bytearray_removesuffix_impl(self, &suffix);
180
181 exit:
182 /* Cleanup for suffix */
183 if (suffix.obj) {
184 PyBuffer_Release(&suffix);
185 }
186
187 return return_value;
188 }
189
190 PyDoc_STRVAR(bytearray_translate__doc__,
191 "translate($self, table, /, delete=b\'\')\n"
192 "--\n"
193 "\n"
194 "Return a copy with each character mapped by the given translation table.\n"
195 "\n"
196 " table\n"
197 " Translation table, which must be a bytes object of length 256.\n"
198 "\n"
199 "All characters occurring in the optional argument delete are removed.\n"
200 "The remaining characters are mapped through the given translation table.");
201
202 #define BYTEARRAY_TRANSLATE_METHODDEF \
203 {"translate", (PyCFunction)(void(*)(void))bytearray_translate, METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__},
204
205 static PyObject *
206 bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
207 PyObject *deletechars);
208
209 static PyObject *
bytearray_translate(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)210 bytearray_translate(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
211 {
212 PyObject *return_value = NULL;
213 static const char * const _keywords[] = {"", "delete", NULL};
214 static _PyArg_Parser _parser = {NULL, _keywords, "translate", 0};
215 PyObject *argsbuf[2];
216 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
217 PyObject *table;
218 PyObject *deletechars = NULL;
219
220 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
221 if (!args) {
222 goto exit;
223 }
224 table = args[0];
225 if (!noptargs) {
226 goto skip_optional_pos;
227 }
228 deletechars = args[1];
229 skip_optional_pos:
230 return_value = bytearray_translate_impl(self, table, deletechars);
231
232 exit:
233 return return_value;
234 }
235
236 PyDoc_STRVAR(bytearray_maketrans__doc__,
237 "maketrans(frm, to, /)\n"
238 "--\n"
239 "\n"
240 "Return a translation table useable for the bytes or bytearray translate method.\n"
241 "\n"
242 "The returned table will be one where each byte in frm is mapped to the byte at\n"
243 "the same position in to.\n"
244 "\n"
245 "The bytes objects frm and to must be of the same length.");
246
247 #define BYTEARRAY_MAKETRANS_METHODDEF \
248 {"maketrans", (PyCFunction)(void(*)(void))bytearray_maketrans, METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__},
249
250 static PyObject *
251 bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
252
253 static PyObject *
bytearray_maketrans(void * null,PyObject * const * args,Py_ssize_t nargs)254 bytearray_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
255 {
256 PyObject *return_value = NULL;
257 Py_buffer frm = {NULL, NULL};
258 Py_buffer to = {NULL, NULL};
259
260 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
261 goto exit;
262 }
263 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
264 goto exit;
265 }
266 if (!PyBuffer_IsContiguous(&frm, 'C')) {
267 _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]);
268 goto exit;
269 }
270 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
271 goto exit;
272 }
273 if (!PyBuffer_IsContiguous(&to, 'C')) {
274 _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]);
275 goto exit;
276 }
277 return_value = bytearray_maketrans_impl(&frm, &to);
278
279 exit:
280 /* Cleanup for frm */
281 if (frm.obj) {
282 PyBuffer_Release(&frm);
283 }
284 /* Cleanup for to */
285 if (to.obj) {
286 PyBuffer_Release(&to);
287 }
288
289 return return_value;
290 }
291
292 PyDoc_STRVAR(bytearray_replace__doc__,
293 "replace($self, old, new, count=-1, /)\n"
294 "--\n"
295 "\n"
296 "Return a copy with all occurrences of substring old replaced by new.\n"
297 "\n"
298 " count\n"
299 " Maximum number of occurrences to replace.\n"
300 " -1 (the default value) means replace all occurrences.\n"
301 "\n"
302 "If the optional argument count is given, only the first count occurrences are\n"
303 "replaced.");
304
305 #define BYTEARRAY_REPLACE_METHODDEF \
306 {"replace", (PyCFunction)(void(*)(void))bytearray_replace, METH_FASTCALL, bytearray_replace__doc__},
307
308 static PyObject *
309 bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
310 Py_buffer *new, Py_ssize_t count);
311
312 static PyObject *
bytearray_replace(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs)313 bytearray_replace(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
314 {
315 PyObject *return_value = NULL;
316 Py_buffer old = {NULL, NULL};
317 Py_buffer new = {NULL, NULL};
318 Py_ssize_t count = -1;
319
320 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
321 goto exit;
322 }
323 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
324 goto exit;
325 }
326 if (!PyBuffer_IsContiguous(&old, 'C')) {
327 _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]);
328 goto exit;
329 }
330 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
331 goto exit;
332 }
333 if (!PyBuffer_IsContiguous(&new, 'C')) {
334 _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]);
335 goto exit;
336 }
337 if (nargs < 3) {
338 goto skip_optional;
339 }
340 {
341 Py_ssize_t ival = -1;
342 PyObject *iobj = _PyNumber_Index(args[2]);
343 if (iobj != NULL) {
344 ival = PyLong_AsSsize_t(iobj);
345 Py_DECREF(iobj);
346 }
347 if (ival == -1 && PyErr_Occurred()) {
348 goto exit;
349 }
350 count = ival;
351 }
352 skip_optional:
353 return_value = bytearray_replace_impl(self, &old, &new, count);
354
355 exit:
356 /* Cleanup for old */
357 if (old.obj) {
358 PyBuffer_Release(&old);
359 }
360 /* Cleanup for new */
361 if (new.obj) {
362 PyBuffer_Release(&new);
363 }
364
365 return return_value;
366 }
367
368 PyDoc_STRVAR(bytearray_split__doc__,
369 "split($self, /, sep=None, maxsplit=-1)\n"
370 "--\n"
371 "\n"
372 "Return a list of the sections in the bytearray, using sep as the delimiter.\n"
373 "\n"
374 " sep\n"
375 " The delimiter according which to split the bytearray.\n"
376 " None (the default value) means split on ASCII whitespace characters\n"
377 " (space, tab, return, newline, formfeed, vertical tab).\n"
378 " maxsplit\n"
379 " Maximum number of splits to do.\n"
380 " -1 (the default value) means no limit.");
381
382 #define BYTEARRAY_SPLIT_METHODDEF \
383 {"split", (PyCFunction)(void(*)(void))bytearray_split, METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
384
385 static PyObject *
386 bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
387 Py_ssize_t maxsplit);
388
389 static PyObject *
bytearray_split(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)390 bytearray_split(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
391 {
392 PyObject *return_value = NULL;
393 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
394 static _PyArg_Parser _parser = {NULL, _keywords, "split", 0};
395 PyObject *argsbuf[2];
396 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
397 PyObject *sep = Py_None;
398 Py_ssize_t maxsplit = -1;
399
400 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
401 if (!args) {
402 goto exit;
403 }
404 if (!noptargs) {
405 goto skip_optional_pos;
406 }
407 if (args[0]) {
408 sep = args[0];
409 if (!--noptargs) {
410 goto skip_optional_pos;
411 }
412 }
413 {
414 Py_ssize_t ival = -1;
415 PyObject *iobj = _PyNumber_Index(args[1]);
416 if (iobj != NULL) {
417 ival = PyLong_AsSsize_t(iobj);
418 Py_DECREF(iobj);
419 }
420 if (ival == -1 && PyErr_Occurred()) {
421 goto exit;
422 }
423 maxsplit = ival;
424 }
425 skip_optional_pos:
426 return_value = bytearray_split_impl(self, sep, maxsplit);
427
428 exit:
429 return return_value;
430 }
431
432 PyDoc_STRVAR(bytearray_partition__doc__,
433 "partition($self, sep, /)\n"
434 "--\n"
435 "\n"
436 "Partition the bytearray into three parts using the given separator.\n"
437 "\n"
438 "This will search for the separator sep in the bytearray. If the separator is\n"
439 "found, returns a 3-tuple containing the part before the separator, the\n"
440 "separator itself, and the part after it as new bytearray objects.\n"
441 "\n"
442 "If the separator is not found, returns a 3-tuple containing the copy of the\n"
443 "original bytearray object and two empty bytearray objects.");
444
445 #define BYTEARRAY_PARTITION_METHODDEF \
446 {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
447
448 PyDoc_STRVAR(bytearray_rpartition__doc__,
449 "rpartition($self, sep, /)\n"
450 "--\n"
451 "\n"
452 "Partition the bytearray into three parts using the given separator.\n"
453 "\n"
454 "This will search for the separator sep in the bytearray, starting at the end.\n"
455 "If the separator is found, returns a 3-tuple containing the part before the\n"
456 "separator, the separator itself, and the part after it as new bytearray\n"
457 "objects.\n"
458 "\n"
459 "If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
460 "objects and the copy of the original bytearray object.");
461
462 #define BYTEARRAY_RPARTITION_METHODDEF \
463 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
464
465 PyDoc_STRVAR(bytearray_rsplit__doc__,
466 "rsplit($self, /, sep=None, maxsplit=-1)\n"
467 "--\n"
468 "\n"
469 "Return a list of the sections in the bytearray, using sep as the delimiter.\n"
470 "\n"
471 " sep\n"
472 " The delimiter according which to split the bytearray.\n"
473 " None (the default value) means split on ASCII whitespace characters\n"
474 " (space, tab, return, newline, formfeed, vertical tab).\n"
475 " maxsplit\n"
476 " Maximum number of splits to do.\n"
477 " -1 (the default value) means no limit.\n"
478 "\n"
479 "Splitting is done starting at the end of the bytearray and working to the front.");
480
481 #define BYTEARRAY_RSPLIT_METHODDEF \
482 {"rsplit", (PyCFunction)(void(*)(void))bytearray_rsplit, METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
483
484 static PyObject *
485 bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
486 Py_ssize_t maxsplit);
487
488 static PyObject *
bytearray_rsplit(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)489 bytearray_rsplit(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
490 {
491 PyObject *return_value = NULL;
492 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
493 static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
494 PyObject *argsbuf[2];
495 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
496 PyObject *sep = Py_None;
497 Py_ssize_t maxsplit = -1;
498
499 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
500 if (!args) {
501 goto exit;
502 }
503 if (!noptargs) {
504 goto skip_optional_pos;
505 }
506 if (args[0]) {
507 sep = args[0];
508 if (!--noptargs) {
509 goto skip_optional_pos;
510 }
511 }
512 {
513 Py_ssize_t ival = -1;
514 PyObject *iobj = _PyNumber_Index(args[1]);
515 if (iobj != NULL) {
516 ival = PyLong_AsSsize_t(iobj);
517 Py_DECREF(iobj);
518 }
519 if (ival == -1 && PyErr_Occurred()) {
520 goto exit;
521 }
522 maxsplit = ival;
523 }
524 skip_optional_pos:
525 return_value = bytearray_rsplit_impl(self, sep, maxsplit);
526
527 exit:
528 return return_value;
529 }
530
531 PyDoc_STRVAR(bytearray_reverse__doc__,
532 "reverse($self, /)\n"
533 "--\n"
534 "\n"
535 "Reverse the order of the values in B in place.");
536
537 #define BYTEARRAY_REVERSE_METHODDEF \
538 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
539
540 static PyObject *
541 bytearray_reverse_impl(PyByteArrayObject *self);
542
543 static PyObject *
bytearray_reverse(PyByteArrayObject * self,PyObject * Py_UNUSED (ignored))544 bytearray_reverse(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
545 {
546 return bytearray_reverse_impl(self);
547 }
548
549 PyDoc_STRVAR(bytearray_insert__doc__,
550 "insert($self, index, item, /)\n"
551 "--\n"
552 "\n"
553 "Insert a single item into the bytearray before the given index.\n"
554 "\n"
555 " index\n"
556 " The index where the value is to be inserted.\n"
557 " item\n"
558 " The item to be inserted.");
559
560 #define BYTEARRAY_INSERT_METHODDEF \
561 {"insert", (PyCFunction)(void(*)(void))bytearray_insert, METH_FASTCALL, bytearray_insert__doc__},
562
563 static PyObject *
564 bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
565
566 static PyObject *
bytearray_insert(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs)567 bytearray_insert(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
568 {
569 PyObject *return_value = NULL;
570 Py_ssize_t index;
571 int item;
572
573 if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
574 goto exit;
575 }
576 {
577 Py_ssize_t ival = -1;
578 PyObject *iobj = _PyNumber_Index(args[0]);
579 if (iobj != NULL) {
580 ival = PyLong_AsSsize_t(iobj);
581 Py_DECREF(iobj);
582 }
583 if (ival == -1 && PyErr_Occurred()) {
584 goto exit;
585 }
586 index = ival;
587 }
588 if (!_getbytevalue(args[1], &item)) {
589 goto exit;
590 }
591 return_value = bytearray_insert_impl(self, index, item);
592
593 exit:
594 return return_value;
595 }
596
597 PyDoc_STRVAR(bytearray_append__doc__,
598 "append($self, item, /)\n"
599 "--\n"
600 "\n"
601 "Append a single item to the end of the bytearray.\n"
602 "\n"
603 " item\n"
604 " The item to be appended.");
605
606 #define BYTEARRAY_APPEND_METHODDEF \
607 {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
608
609 static PyObject *
610 bytearray_append_impl(PyByteArrayObject *self, int item);
611
612 static PyObject *
bytearray_append(PyByteArrayObject * self,PyObject * arg)613 bytearray_append(PyByteArrayObject *self, PyObject *arg)
614 {
615 PyObject *return_value = NULL;
616 int item;
617
618 if (!_getbytevalue(arg, &item)) {
619 goto exit;
620 }
621 return_value = bytearray_append_impl(self, item);
622
623 exit:
624 return return_value;
625 }
626
627 PyDoc_STRVAR(bytearray_extend__doc__,
628 "extend($self, iterable_of_ints, /)\n"
629 "--\n"
630 "\n"
631 "Append all the items from the iterator or sequence to the end of the bytearray.\n"
632 "\n"
633 " iterable_of_ints\n"
634 " The iterable of items to append.");
635
636 #define BYTEARRAY_EXTEND_METHODDEF \
637 {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
638
639 PyDoc_STRVAR(bytearray_pop__doc__,
640 "pop($self, index=-1, /)\n"
641 "--\n"
642 "\n"
643 "Remove and return a single item from B.\n"
644 "\n"
645 " index\n"
646 " The index from where to remove the item.\n"
647 " -1 (the default value) means remove the last item.\n"
648 "\n"
649 "If no index argument is given, will pop the last item.");
650
651 #define BYTEARRAY_POP_METHODDEF \
652 {"pop", (PyCFunction)(void(*)(void))bytearray_pop, METH_FASTCALL, bytearray_pop__doc__},
653
654 static PyObject *
655 bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
656
657 static PyObject *
bytearray_pop(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs)658 bytearray_pop(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
659 {
660 PyObject *return_value = NULL;
661 Py_ssize_t index = -1;
662
663 if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
664 goto exit;
665 }
666 if (nargs < 1) {
667 goto skip_optional;
668 }
669 {
670 Py_ssize_t ival = -1;
671 PyObject *iobj = _PyNumber_Index(args[0]);
672 if (iobj != NULL) {
673 ival = PyLong_AsSsize_t(iobj);
674 Py_DECREF(iobj);
675 }
676 if (ival == -1 && PyErr_Occurred()) {
677 goto exit;
678 }
679 index = ival;
680 }
681 skip_optional:
682 return_value = bytearray_pop_impl(self, index);
683
684 exit:
685 return return_value;
686 }
687
688 PyDoc_STRVAR(bytearray_remove__doc__,
689 "remove($self, value, /)\n"
690 "--\n"
691 "\n"
692 "Remove the first occurrence of a value in the bytearray.\n"
693 "\n"
694 " value\n"
695 " The value to remove.");
696
697 #define BYTEARRAY_REMOVE_METHODDEF \
698 {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
699
700 static PyObject *
701 bytearray_remove_impl(PyByteArrayObject *self, int value);
702
703 static PyObject *
bytearray_remove(PyByteArrayObject * self,PyObject * arg)704 bytearray_remove(PyByteArrayObject *self, PyObject *arg)
705 {
706 PyObject *return_value = NULL;
707 int value;
708
709 if (!_getbytevalue(arg, &value)) {
710 goto exit;
711 }
712 return_value = bytearray_remove_impl(self, value);
713
714 exit:
715 return return_value;
716 }
717
718 PyDoc_STRVAR(bytearray_strip__doc__,
719 "strip($self, bytes=None, /)\n"
720 "--\n"
721 "\n"
722 "Strip leading and trailing bytes contained in the argument.\n"
723 "\n"
724 "If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
725
726 #define BYTEARRAY_STRIP_METHODDEF \
727 {"strip", (PyCFunction)(void(*)(void))bytearray_strip, METH_FASTCALL, bytearray_strip__doc__},
728
729 static PyObject *
730 bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
731
732 static PyObject *
bytearray_strip(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs)733 bytearray_strip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
734 {
735 PyObject *return_value = NULL;
736 PyObject *bytes = Py_None;
737
738 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
739 goto exit;
740 }
741 if (nargs < 1) {
742 goto skip_optional;
743 }
744 bytes = args[0];
745 skip_optional:
746 return_value = bytearray_strip_impl(self, bytes);
747
748 exit:
749 return return_value;
750 }
751
752 PyDoc_STRVAR(bytearray_lstrip__doc__,
753 "lstrip($self, bytes=None, /)\n"
754 "--\n"
755 "\n"
756 "Strip leading bytes contained in the argument.\n"
757 "\n"
758 "If the argument is omitted or None, strip leading ASCII whitespace.");
759
760 #define BYTEARRAY_LSTRIP_METHODDEF \
761 {"lstrip", (PyCFunction)(void(*)(void))bytearray_lstrip, METH_FASTCALL, bytearray_lstrip__doc__},
762
763 static PyObject *
764 bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
765
766 static PyObject *
bytearray_lstrip(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs)767 bytearray_lstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
768 {
769 PyObject *return_value = NULL;
770 PyObject *bytes = Py_None;
771
772 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
773 goto exit;
774 }
775 if (nargs < 1) {
776 goto skip_optional;
777 }
778 bytes = args[0];
779 skip_optional:
780 return_value = bytearray_lstrip_impl(self, bytes);
781
782 exit:
783 return return_value;
784 }
785
786 PyDoc_STRVAR(bytearray_rstrip__doc__,
787 "rstrip($self, bytes=None, /)\n"
788 "--\n"
789 "\n"
790 "Strip trailing bytes contained in the argument.\n"
791 "\n"
792 "If the argument is omitted or None, strip trailing ASCII whitespace.");
793
794 #define BYTEARRAY_RSTRIP_METHODDEF \
795 {"rstrip", (PyCFunction)(void(*)(void))bytearray_rstrip, METH_FASTCALL, bytearray_rstrip__doc__},
796
797 static PyObject *
798 bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
799
800 static PyObject *
bytearray_rstrip(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs)801 bytearray_rstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
802 {
803 PyObject *return_value = NULL;
804 PyObject *bytes = Py_None;
805
806 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
807 goto exit;
808 }
809 if (nargs < 1) {
810 goto skip_optional;
811 }
812 bytes = args[0];
813 skip_optional:
814 return_value = bytearray_rstrip_impl(self, bytes);
815
816 exit:
817 return return_value;
818 }
819
820 PyDoc_STRVAR(bytearray_decode__doc__,
821 "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
822 "--\n"
823 "\n"
824 "Decode the bytearray using the codec registered for encoding.\n"
825 "\n"
826 " encoding\n"
827 " The encoding with which to decode the bytearray.\n"
828 " errors\n"
829 " The error handling scheme to use for the handling of decoding errors.\n"
830 " The default is \'strict\' meaning that decoding errors raise a\n"
831 " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
832 " as well as any other name registered with codecs.register_error that\n"
833 " can handle UnicodeDecodeErrors.");
834
835 #define BYTEARRAY_DECODE_METHODDEF \
836 {"decode", (PyCFunction)(void(*)(void))bytearray_decode, METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
837
838 static PyObject *
839 bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
840 const char *errors);
841
842 static PyObject *
bytearray_decode(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)843 bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
844 {
845 PyObject *return_value = NULL;
846 static const char * const _keywords[] = {"encoding", "errors", NULL};
847 static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0};
848 PyObject *argsbuf[2];
849 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
850 const char *encoding = NULL;
851 const char *errors = NULL;
852
853 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
854 if (!args) {
855 goto exit;
856 }
857 if (!noptargs) {
858 goto skip_optional_pos;
859 }
860 if (args[0]) {
861 if (!PyUnicode_Check(args[0])) {
862 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
863 goto exit;
864 }
865 Py_ssize_t encoding_length;
866 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
867 if (encoding == NULL) {
868 goto exit;
869 }
870 if (strlen(encoding) != (size_t)encoding_length) {
871 PyErr_SetString(PyExc_ValueError, "embedded null character");
872 goto exit;
873 }
874 if (!--noptargs) {
875 goto skip_optional_pos;
876 }
877 }
878 if (!PyUnicode_Check(args[1])) {
879 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
880 goto exit;
881 }
882 Py_ssize_t errors_length;
883 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
884 if (errors == NULL) {
885 goto exit;
886 }
887 if (strlen(errors) != (size_t)errors_length) {
888 PyErr_SetString(PyExc_ValueError, "embedded null character");
889 goto exit;
890 }
891 skip_optional_pos:
892 return_value = bytearray_decode_impl(self, encoding, errors);
893
894 exit:
895 return return_value;
896 }
897
898 PyDoc_STRVAR(bytearray_join__doc__,
899 "join($self, iterable_of_bytes, /)\n"
900 "--\n"
901 "\n"
902 "Concatenate any number of bytes/bytearray objects.\n"
903 "\n"
904 "The bytearray whose method is called is inserted in between each pair.\n"
905 "\n"
906 "The result is returned as a new bytearray object.");
907
908 #define BYTEARRAY_JOIN_METHODDEF \
909 {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
910
911 PyDoc_STRVAR(bytearray_splitlines__doc__,
912 "splitlines($self, /, keepends=False)\n"
913 "--\n"
914 "\n"
915 "Return a list of the lines in the bytearray, breaking at line boundaries.\n"
916 "\n"
917 "Line breaks are not included in the resulting list unless keepends is given and\n"
918 "true.");
919
920 #define BYTEARRAY_SPLITLINES_METHODDEF \
921 {"splitlines", (PyCFunction)(void(*)(void))bytearray_splitlines, METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
922
923 static PyObject *
924 bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
925
926 static PyObject *
bytearray_splitlines(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)927 bytearray_splitlines(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
928 {
929 PyObject *return_value = NULL;
930 static const char * const _keywords[] = {"keepends", NULL};
931 static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
932 PyObject *argsbuf[1];
933 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
934 int keepends = 0;
935
936 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
937 if (!args) {
938 goto exit;
939 }
940 if (!noptargs) {
941 goto skip_optional_pos;
942 }
943 keepends = _PyLong_AsInt(args[0]);
944 if (keepends == -1 && PyErr_Occurred()) {
945 goto exit;
946 }
947 skip_optional_pos:
948 return_value = bytearray_splitlines_impl(self, keepends);
949
950 exit:
951 return return_value;
952 }
953
954 PyDoc_STRVAR(bytearray_fromhex__doc__,
955 "fromhex($type, string, /)\n"
956 "--\n"
957 "\n"
958 "Create a bytearray object from a string of hexadecimal numbers.\n"
959 "\n"
960 "Spaces between two numbers are accepted.\n"
961 "Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
962
963 #define BYTEARRAY_FROMHEX_METHODDEF \
964 {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
965
966 static PyObject *
967 bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
968
969 static PyObject *
bytearray_fromhex(PyTypeObject * type,PyObject * arg)970 bytearray_fromhex(PyTypeObject *type, PyObject *arg)
971 {
972 PyObject *return_value = NULL;
973 PyObject *string;
974
975 if (!PyUnicode_Check(arg)) {
976 _PyArg_BadArgument("fromhex", "argument", "str", arg);
977 goto exit;
978 }
979 if (PyUnicode_READY(arg) == -1) {
980 goto exit;
981 }
982 string = arg;
983 return_value = bytearray_fromhex_impl(type, string);
984
985 exit:
986 return return_value;
987 }
988
989 PyDoc_STRVAR(bytearray_hex__doc__,
990 "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
991 "--\n"
992 "\n"
993 "Create a string of hexadecimal numbers from a bytearray object.\n"
994 "\n"
995 " sep\n"
996 " An optional single character or byte to separate hex bytes.\n"
997 " bytes_per_sep\n"
998 " How many bytes between separators. Positive values count from the\n"
999 " right, negative values count from the left.\n"
1000 "\n"
1001 "Example:\n"
1002 ">>> value = bytearray([0xb9, 0x01, 0xef])\n"
1003 ">>> value.hex()\n"
1004 "\'b901ef\'\n"
1005 ">>> value.hex(\':\')\n"
1006 "\'b9:01:ef\'\n"
1007 ">>> value.hex(\':\', 2)\n"
1008 "\'b9:01ef\'\n"
1009 ">>> value.hex(\':\', -2)\n"
1010 "\'b901:ef\'");
1011
1012 #define BYTEARRAY_HEX_METHODDEF \
1013 {"hex", (PyCFunction)(void(*)(void))bytearray_hex, METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__},
1014
1015 static PyObject *
1016 bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep);
1017
1018 static PyObject *
bytearray_hex(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)1019 bytearray_hex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1020 {
1021 PyObject *return_value = NULL;
1022 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
1023 static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0};
1024 PyObject *argsbuf[2];
1025 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1026 PyObject *sep = NULL;
1027 int bytes_per_sep = 1;
1028
1029 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
1030 if (!args) {
1031 goto exit;
1032 }
1033 if (!noptargs) {
1034 goto skip_optional_pos;
1035 }
1036 if (args[0]) {
1037 sep = args[0];
1038 if (!--noptargs) {
1039 goto skip_optional_pos;
1040 }
1041 }
1042 bytes_per_sep = _PyLong_AsInt(args[1]);
1043 if (bytes_per_sep == -1 && PyErr_Occurred()) {
1044 goto exit;
1045 }
1046 skip_optional_pos:
1047 return_value = bytearray_hex_impl(self, sep, bytes_per_sep);
1048
1049 exit:
1050 return return_value;
1051 }
1052
1053 PyDoc_STRVAR(bytearray_reduce__doc__,
1054 "__reduce__($self, /)\n"
1055 "--\n"
1056 "\n"
1057 "Return state information for pickling.");
1058
1059 #define BYTEARRAY_REDUCE_METHODDEF \
1060 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
1061
1062 static PyObject *
1063 bytearray_reduce_impl(PyByteArrayObject *self);
1064
1065 static PyObject *
bytearray_reduce(PyByteArrayObject * self,PyObject * Py_UNUSED (ignored))1066 bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1067 {
1068 return bytearray_reduce_impl(self);
1069 }
1070
1071 PyDoc_STRVAR(bytearray_reduce_ex__doc__,
1072 "__reduce_ex__($self, proto=0, /)\n"
1073 "--\n"
1074 "\n"
1075 "Return state information for pickling.");
1076
1077 #define BYTEARRAY_REDUCE_EX_METHODDEF \
1078 {"__reduce_ex__", (PyCFunction)(void(*)(void))bytearray_reduce_ex, METH_FASTCALL, bytearray_reduce_ex__doc__},
1079
1080 static PyObject *
1081 bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
1082
1083 static PyObject *
bytearray_reduce_ex(PyByteArrayObject * self,PyObject * const * args,Py_ssize_t nargs)1084 bytearray_reduce_ex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
1085 {
1086 PyObject *return_value = NULL;
1087 int proto = 0;
1088
1089 if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
1090 goto exit;
1091 }
1092 if (nargs < 1) {
1093 goto skip_optional;
1094 }
1095 proto = _PyLong_AsInt(args[0]);
1096 if (proto == -1 && PyErr_Occurred()) {
1097 goto exit;
1098 }
1099 skip_optional:
1100 return_value = bytearray_reduce_ex_impl(self, proto);
1101
1102 exit:
1103 return return_value;
1104 }
1105
1106 PyDoc_STRVAR(bytearray_sizeof__doc__,
1107 "__sizeof__($self, /)\n"
1108 "--\n"
1109 "\n"
1110 "Returns the size of the bytearray object in memory, in bytes.");
1111
1112 #define BYTEARRAY_SIZEOF_METHODDEF \
1113 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
1114
1115 static PyObject *
1116 bytearray_sizeof_impl(PyByteArrayObject *self);
1117
1118 static PyObject *
bytearray_sizeof(PyByteArrayObject * self,PyObject * Py_UNUSED (ignored))1119 bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1120 {
1121 return bytearray_sizeof_impl(self);
1122 }
1123 /*[clinic end generated code: output=a82659f581e55629 input=a9049054013a1b77]*/
1124