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