blob: cbe6f20344e8c95a58ccafb03095a6a526d54e14 [file] [log] [blame]
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_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
14static PyObject *
15bytearray_clear_impl(PyByteArrayObject *self);
16
17static PyObject *
18bytearray_clear(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
19{
20 return bytearray_clear_impl(self);
21}
22
23PyDoc_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
32static PyObject *
33bytearray_copy_impl(PyByteArrayObject *self);
34
35static PyObject *
36bytearray_copy(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
37{
38 return bytearray_copy_impl(self);
39}
40
sweeneydea81849b2020-04-22 17:05:48 -040041PyDoc_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
54static PyObject *
55bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix);
56
57static PyObject *
58bytearray_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
72exit:
73 /* Cleanup for prefix */
74 if (prefix.obj) {
75 PyBuffer_Release(&prefix);
76 }
77
78 return return_value;
79}
80
81PyDoc_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
94static PyObject *
95bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix);
96
97static PyObject *
98bytearray_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
112exit:
113 /* Cleanup for suffix */
114 if (suffix.obj) {
115 PyBuffer_Release(&suffix);
116 }
117
118 return return_value;
119}
120
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300121PyDoc_STRVAR(bytearray_translate__doc__,
Martin Panter1b6c6da2016-08-27 08:35:02 +0000122"translate($self, table, /, delete=b\'\')\n"
123"--\n"
124"\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300125"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"
Martin Panter1b6c6da2016-08-27 08:35:02 +0000130"All characters occurring in the optional argument delete are removed.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300131"The remaining characters are mapped through the given translation table.");
132
133#define BYTEARRAY_TRANSLATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200134 {"translate", (PyCFunction)(void(*)(void))bytearray_translate, METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300135
136static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400137bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +0000138 PyObject *deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300139
140static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200141bytearray_translate(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300142{
143 PyObject *return_value = NULL;
Martin Panter1b6c6da2016-08-27 08:35:02 +0000144 static const char * const _keywords[] = {"", "delete", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200145 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;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300148 PyObject *table;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300149 PyObject *deletechars = NULL;
150
Serhiy Storchaka31913912019-03-14 10:32:22 +0200151 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
152 if (!args) {
Martin Panter1b6c6da2016-08-27 08:35:02 +0000153 goto exit;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300154 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200155 table = args[0];
156 if (!noptargs) {
157 goto skip_optional_pos;
158 }
159 deletechars = args[1];
160skip_optional_pos:
Martin Panter1b6c6da2016-08-27 08:35:02 +0000161 return_value = bytearray_translate_impl(self, table, deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300162
163exit:
164 return return_value;
165}
166
167PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200179 {"maketrans", (PyCFunction)(void(*)(void))bytearray_maketrans, METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300180
181static PyObject *
182bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
183
184static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200185bytearray_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300186{
187 PyObject *return_value = NULL;
188 Py_buffer frm = {NULL, NULL};
189 Py_buffer to = {NULL, NULL};
190
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200191 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')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200198 _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200199 goto exit;
200 }
201 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
202 goto exit;
203 }
204 if (!PyBuffer_IsContiguous(&to, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200205 _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100206 goto exit;
207 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300208 return_value = bytearray_maketrans_impl(&frm, &to);
209
210exit:
211 /* Cleanup for frm */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300212 if (frm.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300213 PyBuffer_Release(&frm);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300214 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300215 /* Cleanup for to */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300216 if (to.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300217 PyBuffer_Release(&to);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300218 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300219
220 return return_value;
221}
222
223PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200237 {"replace", (PyCFunction)(void(*)(void))bytearray_replace, METH_FASTCALL, bytearray_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300238
239static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400240bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
241 Py_buffer *new, Py_ssize_t count);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300242
243static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200244bytearray_replace(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300245{
246 PyObject *return_value = NULL;
247 Py_buffer old = {NULL, NULL};
248 Py_buffer new = {NULL, NULL};
249 Py_ssize_t count = -1;
250
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200251 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100252 goto exit;
253 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200254 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
255 goto exit;
256 }
257 if (!PyBuffer_IsContiguous(&old, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200258 _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200259 goto exit;
260 }
261 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
262 goto exit;
263 }
264 if (!PyBuffer_IsContiguous(&new, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200265 _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200266 goto exit;
267 }
268 if (nargs < 3) {
269 goto skip_optional;
270 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200271 {
272 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300273 PyObject *iobj = _PyNumber_Index(args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200274 if (iobj != NULL) {
275 ival = PyLong_AsSsize_t(iobj);
276 Py_DECREF(iobj);
277 }
278 if (ival == -1 && PyErr_Occurred()) {
279 goto exit;
280 }
281 count = ival;
282 }
283skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300284 return_value = bytearray_replace_impl(self, &old, &new, count);
285
286exit:
287 /* Cleanup for old */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300288 if (old.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300289 PyBuffer_Release(&old);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300290 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300291 /* Cleanup for new */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300292 if (new.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300293 PyBuffer_Release(&new);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300294 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300295
296 return return_value;
297}
298
299PyDoc_STRVAR(bytearray_split__doc__,
300"split($self, /, sep=None, maxsplit=-1)\n"
301"--\n"
302"\n"
303"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
304"\n"
305" sep\n"
306" The delimiter according which to split the bytearray.\n"
307" None (the default value) means split on ASCII whitespace characters\n"
308" (space, tab, return, newline, formfeed, vertical tab).\n"
309" maxsplit\n"
310" Maximum number of splits to do.\n"
311" -1 (the default value) means no limit.");
312
313#define BYTEARRAY_SPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200314 {"split", (PyCFunction)(void(*)(void))bytearray_split, METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300315
316static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400317bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
318 Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300319
320static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200321bytearray_split(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300322{
323 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300324 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200325 static _PyArg_Parser _parser = {NULL, _keywords, "split", 0};
326 PyObject *argsbuf[2];
327 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300328 PyObject *sep = Py_None;
329 Py_ssize_t maxsplit = -1;
330
Serhiy Storchaka31913912019-03-14 10:32:22 +0200331 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
332 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300333 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300334 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200335 if (!noptargs) {
336 goto skip_optional_pos;
337 }
338 if (args[0]) {
339 sep = args[0];
340 if (!--noptargs) {
341 goto skip_optional_pos;
342 }
343 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200344 {
345 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300346 PyObject *iobj = _PyNumber_Index(args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200347 if (iobj != NULL) {
348 ival = PyLong_AsSsize_t(iobj);
349 Py_DECREF(iobj);
350 }
351 if (ival == -1 && PyErr_Occurred()) {
352 goto exit;
353 }
354 maxsplit = ival;
355 }
356skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300357 return_value = bytearray_split_impl(self, sep, maxsplit);
358
359exit:
360 return return_value;
361}
362
363PyDoc_STRVAR(bytearray_partition__doc__,
364"partition($self, sep, /)\n"
365"--\n"
366"\n"
367"Partition the bytearray into three parts using the given separator.\n"
368"\n"
369"This will search for the separator sep in the bytearray. If the separator is\n"
370"found, returns a 3-tuple containing the part before the separator, the\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300371"separator itself, and the part after it as new bytearray objects.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300372"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300373"If the separator is not found, returns a 3-tuple containing the copy of the\n"
374"original bytearray object and two empty bytearray objects.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300375
376#define BYTEARRAY_PARTITION_METHODDEF \
377 {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
378
379PyDoc_STRVAR(bytearray_rpartition__doc__,
380"rpartition($self, sep, /)\n"
381"--\n"
382"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300383"Partition the bytearray into three parts using the given separator.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300384"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300385"This will search for the separator sep in the bytearray, starting at the end.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300386"If the separator is found, returns a 3-tuple containing the part before the\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300387"separator, the separator itself, and the part after it as new bytearray\n"
388"objects.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300389"\n"
390"If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300391"objects and the copy of the original bytearray object.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300392
393#define BYTEARRAY_RPARTITION_METHODDEF \
394 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
395
396PyDoc_STRVAR(bytearray_rsplit__doc__,
397"rsplit($self, /, sep=None, maxsplit=-1)\n"
398"--\n"
399"\n"
400"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
401"\n"
402" sep\n"
403" The delimiter according which to split the bytearray.\n"
404" None (the default value) means split on ASCII whitespace characters\n"
405" (space, tab, return, newline, formfeed, vertical tab).\n"
406" maxsplit\n"
407" Maximum number of splits to do.\n"
408" -1 (the default value) means no limit.\n"
409"\n"
410"Splitting is done starting at the end of the bytearray and working to the front.");
411
412#define BYTEARRAY_RSPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200413 {"rsplit", (PyCFunction)(void(*)(void))bytearray_rsplit, METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300414
415static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400416bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
417 Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300418
419static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200420bytearray_rsplit(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300421{
422 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300423 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200424 static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
425 PyObject *argsbuf[2];
426 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300427 PyObject *sep = Py_None;
428 Py_ssize_t maxsplit = -1;
429
Serhiy Storchaka31913912019-03-14 10:32:22 +0200430 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
431 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300432 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300433 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200434 if (!noptargs) {
435 goto skip_optional_pos;
436 }
437 if (args[0]) {
438 sep = args[0];
439 if (!--noptargs) {
440 goto skip_optional_pos;
441 }
442 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200443 {
444 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300445 PyObject *iobj = _PyNumber_Index(args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200446 if (iobj != NULL) {
447 ival = PyLong_AsSsize_t(iobj);
448 Py_DECREF(iobj);
449 }
450 if (ival == -1 && PyErr_Occurred()) {
451 goto exit;
452 }
453 maxsplit = ival;
454 }
455skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300456 return_value = bytearray_rsplit_impl(self, sep, maxsplit);
457
458exit:
459 return return_value;
460}
461
462PyDoc_STRVAR(bytearray_reverse__doc__,
463"reverse($self, /)\n"
464"--\n"
465"\n"
466"Reverse the order of the values in B in place.");
467
468#define BYTEARRAY_REVERSE_METHODDEF \
469 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
470
471static PyObject *
472bytearray_reverse_impl(PyByteArrayObject *self);
473
474static PyObject *
475bytearray_reverse(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
476{
477 return bytearray_reverse_impl(self);
478}
479
480PyDoc_STRVAR(bytearray_insert__doc__,
481"insert($self, index, item, /)\n"
482"--\n"
483"\n"
484"Insert a single item into the bytearray before the given index.\n"
485"\n"
486" index\n"
487" The index where the value is to be inserted.\n"
488" item\n"
489" The item to be inserted.");
490
491#define BYTEARRAY_INSERT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200492 {"insert", (PyCFunction)(void(*)(void))bytearray_insert, METH_FASTCALL, bytearray_insert__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300493
494static PyObject *
495bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
496
497static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200498bytearray_insert(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300499{
500 PyObject *return_value = NULL;
501 Py_ssize_t index;
502 int item;
503
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200504 if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
505 goto exit;
506 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200507 {
508 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300509 PyObject *iobj = _PyNumber_Index(args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200510 if (iobj != NULL) {
511 ival = PyLong_AsSsize_t(iobj);
512 Py_DECREF(iobj);
513 }
514 if (ival == -1 && PyErr_Occurred()) {
515 goto exit;
516 }
517 index = ival;
518 }
519 if (!_getbytevalue(args[1], &item)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100520 goto exit;
521 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300522 return_value = bytearray_insert_impl(self, index, item);
523
524exit:
525 return return_value;
526}
527
528PyDoc_STRVAR(bytearray_append__doc__,
529"append($self, item, /)\n"
530"--\n"
531"\n"
532"Append a single item to the end of the bytearray.\n"
533"\n"
534" item\n"
535" The item to be appended.");
536
537#define BYTEARRAY_APPEND_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300538 {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300539
540static PyObject *
541bytearray_append_impl(PyByteArrayObject *self, int item);
542
543static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300544bytearray_append(PyByteArrayObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300545{
546 PyObject *return_value = NULL;
547 int item;
548
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200549 if (!_getbytevalue(arg, &item)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300550 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300551 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300552 return_value = bytearray_append_impl(self, item);
553
554exit:
555 return return_value;
556}
557
558PyDoc_STRVAR(bytearray_extend__doc__,
559"extend($self, iterable_of_ints, /)\n"
560"--\n"
561"\n"
562"Append all the items from the iterator or sequence to the end of the bytearray.\n"
563"\n"
564" iterable_of_ints\n"
565" The iterable of items to append.");
566
567#define BYTEARRAY_EXTEND_METHODDEF \
568 {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
569
570PyDoc_STRVAR(bytearray_pop__doc__,
571"pop($self, index=-1, /)\n"
572"--\n"
573"\n"
574"Remove and return a single item from B.\n"
575"\n"
576" index\n"
577" The index from where to remove the item.\n"
578" -1 (the default value) means remove the last item.\n"
579"\n"
580"If no index argument is given, will pop the last item.");
581
582#define BYTEARRAY_POP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200583 {"pop", (PyCFunction)(void(*)(void))bytearray_pop, METH_FASTCALL, bytearray_pop__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300584
585static PyObject *
586bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
587
588static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200589bytearray_pop(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300590{
591 PyObject *return_value = NULL;
592 Py_ssize_t index = -1;
593
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200594 if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100595 goto exit;
596 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200597 if (nargs < 1) {
598 goto skip_optional;
599 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200600 {
601 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300602 PyObject *iobj = _PyNumber_Index(args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200603 if (iobj != NULL) {
604 ival = PyLong_AsSsize_t(iobj);
605 Py_DECREF(iobj);
606 }
607 if (ival == -1 && PyErr_Occurred()) {
608 goto exit;
609 }
610 index = ival;
611 }
612skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300613 return_value = bytearray_pop_impl(self, index);
614
615exit:
616 return return_value;
617}
618
619PyDoc_STRVAR(bytearray_remove__doc__,
620"remove($self, value, /)\n"
621"--\n"
622"\n"
623"Remove the first occurrence of a value in the bytearray.\n"
624"\n"
625" value\n"
626" The value to remove.");
627
628#define BYTEARRAY_REMOVE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300629 {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300630
631static PyObject *
632bytearray_remove_impl(PyByteArrayObject *self, int value);
633
634static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300635bytearray_remove(PyByteArrayObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300636{
637 PyObject *return_value = NULL;
638 int value;
639
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200640 if (!_getbytevalue(arg, &value)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300641 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300642 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300643 return_value = bytearray_remove_impl(self, value);
644
645exit:
646 return return_value;
647}
648
649PyDoc_STRVAR(bytearray_strip__doc__,
650"strip($self, bytes=None, /)\n"
651"--\n"
652"\n"
653"Strip leading and trailing bytes contained in the argument.\n"
654"\n"
655"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
656
657#define BYTEARRAY_STRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200658 {"strip", (PyCFunction)(void(*)(void))bytearray_strip, METH_FASTCALL, bytearray_strip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300659
660static PyObject *
661bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
662
663static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200664bytearray_strip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300665{
666 PyObject *return_value = NULL;
667 PyObject *bytes = Py_None;
668
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200669 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100670 goto exit;
671 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200672 if (nargs < 1) {
673 goto skip_optional;
674 }
675 bytes = args[0];
676skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300677 return_value = bytearray_strip_impl(self, bytes);
678
679exit:
680 return return_value;
681}
682
683PyDoc_STRVAR(bytearray_lstrip__doc__,
684"lstrip($self, bytes=None, /)\n"
685"--\n"
686"\n"
687"Strip leading bytes contained in the argument.\n"
688"\n"
689"If the argument is omitted or None, strip leading ASCII whitespace.");
690
691#define BYTEARRAY_LSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200692 {"lstrip", (PyCFunction)(void(*)(void))bytearray_lstrip, METH_FASTCALL, bytearray_lstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300693
694static PyObject *
695bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
696
697static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200698bytearray_lstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300699{
700 PyObject *return_value = NULL;
701 PyObject *bytes = Py_None;
702
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200703 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100704 goto exit;
705 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200706 if (nargs < 1) {
707 goto skip_optional;
708 }
709 bytes = args[0];
710skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300711 return_value = bytearray_lstrip_impl(self, bytes);
712
713exit:
714 return return_value;
715}
716
717PyDoc_STRVAR(bytearray_rstrip__doc__,
718"rstrip($self, bytes=None, /)\n"
719"--\n"
720"\n"
721"Strip trailing bytes contained in the argument.\n"
722"\n"
723"If the argument is omitted or None, strip trailing ASCII whitespace.");
724
725#define BYTEARRAY_RSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200726 {"rstrip", (PyCFunction)(void(*)(void))bytearray_rstrip, METH_FASTCALL, bytearray_rstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300727
728static PyObject *
729bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
730
731static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200732bytearray_rstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300733{
734 PyObject *return_value = NULL;
735 PyObject *bytes = Py_None;
736
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200737 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100738 goto exit;
739 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200740 if (nargs < 1) {
741 goto skip_optional;
742 }
743 bytes = args[0];
744skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300745 return_value = bytearray_rstrip_impl(self, bytes);
746
747exit:
748 return return_value;
749}
750
751PyDoc_STRVAR(bytearray_decode__doc__,
752"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
753"--\n"
754"\n"
755"Decode the bytearray using the codec registered for encoding.\n"
756"\n"
757" encoding\n"
758" The encoding with which to decode the bytearray.\n"
759" errors\n"
760" The error handling scheme to use for the handling of decoding errors.\n"
761" The default is \'strict\' meaning that decoding errors raise a\n"
762" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
763" as well as any other name registered with codecs.register_error that\n"
764" can handle UnicodeDecodeErrors.");
765
766#define BYTEARRAY_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200767 {"decode", (PyCFunction)(void(*)(void))bytearray_decode, METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300768
769static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400770bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
771 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300772
773static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200774bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300775{
776 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300777 static const char * const _keywords[] = {"encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200778 static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0};
779 PyObject *argsbuf[2];
780 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300781 const char *encoding = NULL;
782 const char *errors = NULL;
783
Serhiy Storchaka31913912019-03-14 10:32:22 +0200784 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
785 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300786 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300787 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200788 if (!noptargs) {
789 goto skip_optional_pos;
790 }
791 if (args[0]) {
792 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200793 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200794 goto exit;
795 }
796 Py_ssize_t encoding_length;
797 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
798 if (encoding == NULL) {
799 goto exit;
800 }
801 if (strlen(encoding) != (size_t)encoding_length) {
802 PyErr_SetString(PyExc_ValueError, "embedded null character");
803 goto exit;
804 }
805 if (!--noptargs) {
806 goto skip_optional_pos;
807 }
808 }
809 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200810 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200811 goto exit;
812 }
813 Py_ssize_t errors_length;
814 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
815 if (errors == NULL) {
816 goto exit;
817 }
818 if (strlen(errors) != (size_t)errors_length) {
819 PyErr_SetString(PyExc_ValueError, "embedded null character");
820 goto exit;
821 }
822skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300823 return_value = bytearray_decode_impl(self, encoding, errors);
824
825exit:
826 return return_value;
827}
828
829PyDoc_STRVAR(bytearray_join__doc__,
830"join($self, iterable_of_bytes, /)\n"
831"--\n"
832"\n"
833"Concatenate any number of bytes/bytearray objects.\n"
834"\n"
835"The bytearray whose method is called is inserted in between each pair.\n"
836"\n"
837"The result is returned as a new bytearray object.");
838
839#define BYTEARRAY_JOIN_METHODDEF \
840 {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
841
842PyDoc_STRVAR(bytearray_splitlines__doc__,
843"splitlines($self, /, keepends=False)\n"
844"--\n"
845"\n"
846"Return a list of the lines in the bytearray, breaking at line boundaries.\n"
847"\n"
848"Line breaks are not included in the resulting list unless keepends is given and\n"
849"true.");
850
851#define BYTEARRAY_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200852 {"splitlines", (PyCFunction)(void(*)(void))bytearray_splitlines, METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300853
854static PyObject *
855bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
856
857static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200858bytearray_splitlines(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300859{
860 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300861 static const char * const _keywords[] = {"keepends", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200862 static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
863 PyObject *argsbuf[1];
864 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300865 int keepends = 0;
866
Serhiy Storchaka31913912019-03-14 10:32:22 +0200867 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
868 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300869 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300870 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200871 if (!noptargs) {
872 goto skip_optional_pos;
873 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200874 keepends = _PyLong_AsInt(args[0]);
875 if (keepends == -1 && PyErr_Occurred()) {
876 goto exit;
877 }
878skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300879 return_value = bytearray_splitlines_impl(self, keepends);
880
881exit:
882 return return_value;
883}
884
885PyDoc_STRVAR(bytearray_fromhex__doc__,
886"fromhex($type, string, /)\n"
887"--\n"
888"\n"
889"Create a bytearray object from a string of hexadecimal numbers.\n"
890"\n"
891"Spaces between two numbers are accepted.\n"
892"Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
893
894#define BYTEARRAY_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300895 {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300896
897static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300898bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300899
900static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300901bytearray_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300902{
903 PyObject *return_value = NULL;
904 PyObject *string;
905
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200906 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200907 _PyArg_BadArgument("fromhex", "argument", "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300908 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300909 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200910 if (PyUnicode_READY(arg) == -1) {
911 goto exit;
912 }
913 string = arg;
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300914 return_value = bytearray_fromhex_impl(type, string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300915
916exit:
917 return return_value;
918}
919
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700920PyDoc_STRVAR(bytearray_hex__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300921"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700922"--\n"
923"\n"
924"Create a str of hexadecimal numbers from a bytearray object.\n"
925"\n"
926" sep\n"
927" An optional single character or byte to separate hex bytes.\n"
928" bytes_per_sep\n"
929" How many bytes between separators. Positive values count from the\n"
930" right, negative values count from the left.\n"
931"\n"
932"Example:\n"
933">>> value = bytearray([0xb9, 0x01, 0xef])\n"
934">>> value.hex()\n"
935"\'b901ef\'\n"
936">>> value.hex(\':\')\n"
937"\'b9:01:ef\'\n"
938">>> value.hex(\':\', 2)\n"
939"\'b9:01ef\'\n"
940">>> value.hex(\':\', -2)\n"
941"\'b901:ef\'");
942
943#define BYTEARRAY_HEX_METHODDEF \
944 {"hex", (PyCFunction)(void(*)(void))bytearray_hex, METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__},
945
946static PyObject *
947bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep);
948
949static PyObject *
950bytearray_hex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
951{
952 PyObject *return_value = NULL;
953 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
954 static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0};
955 PyObject *argsbuf[2];
956 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
957 PyObject *sep = NULL;
958 int bytes_per_sep = 1;
959
960 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
961 if (!args) {
962 goto exit;
963 }
964 if (!noptargs) {
965 goto skip_optional_pos;
966 }
967 if (args[0]) {
968 sep = args[0];
969 if (!--noptargs) {
970 goto skip_optional_pos;
971 }
972 }
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700973 bytes_per_sep = _PyLong_AsInt(args[1]);
974 if (bytes_per_sep == -1 && PyErr_Occurred()) {
975 goto exit;
976 }
977skip_optional_pos:
978 return_value = bytearray_hex_impl(self, sep, bytes_per_sep);
979
980exit:
981 return return_value;
982}
983
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300984PyDoc_STRVAR(bytearray_reduce__doc__,
985"__reduce__($self, /)\n"
986"--\n"
987"\n"
988"Return state information for pickling.");
989
990#define BYTEARRAY_REDUCE_METHODDEF \
991 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
992
993static PyObject *
994bytearray_reduce_impl(PyByteArrayObject *self);
995
996static PyObject *
997bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
998{
999 return bytearray_reduce_impl(self);
1000}
1001
1002PyDoc_STRVAR(bytearray_reduce_ex__doc__,
1003"__reduce_ex__($self, proto=0, /)\n"
1004"--\n"
1005"\n"
1006"Return state information for pickling.");
1007
1008#define BYTEARRAY_REDUCE_EX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001009 {"__reduce_ex__", (PyCFunction)(void(*)(void))bytearray_reduce_ex, METH_FASTCALL, bytearray_reduce_ex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001010
1011static PyObject *
1012bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
1013
1014static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001015bytearray_reduce_ex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001016{
1017 PyObject *return_value = NULL;
1018 int proto = 0;
1019
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001020 if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001021 goto exit;
1022 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001023 if (nargs < 1) {
1024 goto skip_optional;
1025 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001026 proto = _PyLong_AsInt(args[0]);
1027 if (proto == -1 && PyErr_Occurred()) {
1028 goto exit;
1029 }
1030skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001031 return_value = bytearray_reduce_ex_impl(self, proto);
1032
1033exit:
1034 return return_value;
1035}
1036
1037PyDoc_STRVAR(bytearray_sizeof__doc__,
1038"__sizeof__($self, /)\n"
1039"--\n"
1040"\n"
1041"Returns the size of the bytearray object in memory, in bytes.");
1042
1043#define BYTEARRAY_SIZEOF_METHODDEF \
1044 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
1045
1046static PyObject *
1047bytearray_sizeof_impl(PyByteArrayObject *self);
1048
1049static PyObject *
1050bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1051{
1052 return bytearray_sizeof_impl(self);
1053}
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +03001054/*[clinic end generated code: output=0cd59180c7d5dce5 input=a9049054013a1b77]*/