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