blob: 35ba1ff3d576d2c7e318aee9ba66c77df6cb30f6 [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 }
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 }
288skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300289 return_value = bytearray_replace_impl(self, &old, &new, count);
290
291exit:
292 /* Cleanup for old */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300293 if (old.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300294 PyBuffer_Release(&old);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300295 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300296 /* Cleanup for new */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300297 if (new.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300298 PyBuffer_Release(&new);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300299 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300300
301 return return_value;
302}
303
304PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200319 {"split", (PyCFunction)(void(*)(void))bytearray_split, METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300320
321static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400322bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
323 Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300324
325static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200326bytearray_split(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300327{
328 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300329 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200330 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;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300333 PyObject *sep = Py_None;
334 Py_ssize_t maxsplit = -1;
335
Serhiy Storchaka31913912019-03-14 10:32:22 +0200336 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
337 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300338 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300339 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200340 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 }
366skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300367 return_value = bytearray_split_impl(self, sep, maxsplit);
368
369exit:
370 return return_value;
371}
372
373PyDoc_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"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300381"separator itself, and the part after it as new bytearray objects.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300382"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300383"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.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300385
386#define BYTEARRAY_PARTITION_METHODDEF \
387 {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
388
389PyDoc_STRVAR(bytearray_rpartition__doc__,
390"rpartition($self, sep, /)\n"
391"--\n"
392"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300393"Partition the bytearray into three parts using the given separator.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300394"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300395"This will search for the separator sep in the bytearray, starting at the end.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300396"If the separator is found, returns a 3-tuple containing the part before the\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300397"separator, the separator itself, and the part after it as new bytearray\n"
398"objects.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300399"\n"
400"If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300401"objects and the copy of the original bytearray object.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300402
403#define BYTEARRAY_RPARTITION_METHODDEF \
404 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
405
406PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200423 {"rsplit", (PyCFunction)(void(*)(void))bytearray_rsplit, METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300424
425static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400426bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
427 Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300428
429static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200430bytearray_rsplit(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300431{
432 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300433 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200434 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;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300437 PyObject *sep = Py_None;
438 Py_ssize_t maxsplit = -1;
439
Serhiy Storchaka31913912019-03-14 10:32:22 +0200440 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
441 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300442 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300443 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200444 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 }
470skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300471 return_value = bytearray_rsplit_impl(self, sep, maxsplit);
472
473exit:
474 return return_value;
475}
476
477PyDoc_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
486static PyObject *
487bytearray_reverse_impl(PyByteArrayObject *self);
488
489static PyObject *
490bytearray_reverse(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
491{
492 return bytearray_reverse_impl(self);
493}
494
495PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200507 {"insert", (PyCFunction)(void(*)(void))bytearray_insert, METH_FASTCALL, bytearray_insert__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300508
509static PyObject *
510bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
511
512static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200513bytearray_insert(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300514{
515 PyObject *return_value = NULL;
516 Py_ssize_t index;
517 int item;
518
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200519 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)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100540 goto exit;
541 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300542 return_value = bytearray_insert_impl(self, index, item);
543
544exit:
545 return return_value;
546}
547
548PyDoc_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 \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300558 {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300559
560static PyObject *
561bytearray_append_impl(PyByteArrayObject *self, int item);
562
563static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300564bytearray_append(PyByteArrayObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300565{
566 PyObject *return_value = NULL;
567 int item;
568
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200569 if (!_getbytevalue(arg, &item)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300570 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300571 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300572 return_value = bytearray_append_impl(self, item);
573
574exit:
575 return return_value;
576}
577
578PyDoc_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
590PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200603 {"pop", (PyCFunction)(void(*)(void))bytearray_pop, METH_FASTCALL, bytearray_pop__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300604
605static PyObject *
606bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
607
608static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200609bytearray_pop(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300610{
611 PyObject *return_value = NULL;
612 Py_ssize_t index = -1;
613
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200614 if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100615 goto exit;
616 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200617 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 }
637skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300638 return_value = bytearray_pop_impl(self, index);
639
640exit:
641 return return_value;
642}
643
644PyDoc_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 \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300654 {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300655
656static PyObject *
657bytearray_remove_impl(PyByteArrayObject *self, int value);
658
659static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300660bytearray_remove(PyByteArrayObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300661{
662 PyObject *return_value = NULL;
663 int value;
664
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200665 if (!_getbytevalue(arg, &value)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300666 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300667 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300668 return_value = bytearray_remove_impl(self, value);
669
670exit:
671 return return_value;
672}
673
674PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200683 {"strip", (PyCFunction)(void(*)(void))bytearray_strip, METH_FASTCALL, bytearray_strip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300684
685static PyObject *
686bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
687
688static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200689bytearray_strip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300690{
691 PyObject *return_value = NULL;
692 PyObject *bytes = Py_None;
693
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200694 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100695 goto exit;
696 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200697 if (nargs < 1) {
698 goto skip_optional;
699 }
700 bytes = args[0];
701skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300702 return_value = bytearray_strip_impl(self, bytes);
703
704exit:
705 return return_value;
706}
707
708PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200717 {"lstrip", (PyCFunction)(void(*)(void))bytearray_lstrip, METH_FASTCALL, bytearray_lstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300718
719static PyObject *
720bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
721
722static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200723bytearray_lstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300724{
725 PyObject *return_value = NULL;
726 PyObject *bytes = Py_None;
727
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200728 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100729 goto exit;
730 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200731 if (nargs < 1) {
732 goto skip_optional;
733 }
734 bytes = args[0];
735skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300736 return_value = bytearray_lstrip_impl(self, bytes);
737
738exit:
739 return return_value;
740}
741
742PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200751 {"rstrip", (PyCFunction)(void(*)(void))bytearray_rstrip, METH_FASTCALL, bytearray_rstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300752
753static PyObject *
754bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
755
756static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200757bytearray_rstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300758{
759 PyObject *return_value = NULL;
760 PyObject *bytes = Py_None;
761
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200762 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100763 goto exit;
764 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200765 if (nargs < 1) {
766 goto skip_optional;
767 }
768 bytes = args[0];
769skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300770 return_value = bytearray_rstrip_impl(self, bytes);
771
772exit:
773 return return_value;
774}
775
776PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200792 {"decode", (PyCFunction)(void(*)(void))bytearray_decode, METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300793
794static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400795bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
796 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300797
798static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200799bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300800{
801 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300802 static const char * const _keywords[] = {"encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200803 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;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300806 const char *encoding = NULL;
807 const char *errors = NULL;
808
Serhiy Storchaka31913912019-03-14 10:32:22 +0200809 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
810 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300811 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300812 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200813 if (!noptargs) {
814 goto skip_optional_pos;
815 }
816 if (args[0]) {
817 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200818 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200819 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])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200835 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200836 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 }
847skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300848 return_value = bytearray_decode_impl(self, encoding, errors);
849
850exit:
851 return return_value;
852}
853
854PyDoc_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
867PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200877 {"splitlines", (PyCFunction)(void(*)(void))bytearray_splitlines, METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300878
879static PyObject *
880bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
881
882static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200883bytearray_splitlines(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300884{
885 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300886 static const char * const _keywords[] = {"keepends", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200887 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;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300890 int keepends = 0;
891
Serhiy Storchaka31913912019-03-14 10:32:22 +0200892 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
893 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300894 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300895 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200896 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 }
908skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300909 return_value = bytearray_splitlines_impl(self, keepends);
910
911exit:
912 return return_value;
913}
914
915PyDoc_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 \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300925 {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300926
927static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300928bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300929
930static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300931bytearray_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300932{
933 PyObject *return_value = NULL;
934 PyObject *string;
935
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200936 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200937 _PyArg_BadArgument("fromhex", "argument", "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300938 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300939 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200940 if (PyUnicode_READY(arg) == -1) {
941 goto exit;
942 }
943 string = arg;
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300944 return_value = bytearray_fromhex_impl(type, string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300945
946exit:
947 return return_value;
948}
949
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700950PyDoc_STRVAR(bytearray_hex__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300951"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700952"--\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
976static PyObject *
977bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep);
978
979static PyObject *
980bytearray_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 }
1012skip_optional_pos:
1013 return_value = bytearray_hex_impl(self, sep, bytes_per_sep);
1014
1015exit:
1016 return return_value;
1017}
1018
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001019PyDoc_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
1028static PyObject *
1029bytearray_reduce_impl(PyByteArrayObject *self);
1030
1031static PyObject *
1032bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1033{
1034 return bytearray_reduce_impl(self);
1035}
1036
1037PyDoc_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 \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001044 {"__reduce_ex__", (PyCFunction)(void(*)(void))bytearray_reduce_ex, METH_FASTCALL, bytearray_reduce_ex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001045
1046static PyObject *
1047bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
1048
1049static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001050bytearray_reduce_ex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001051{
1052 PyObject *return_value = NULL;
1053 int proto = 0;
1054
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001055 if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001056 goto exit;
1057 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001058 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 }
1070skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001071 return_value = bytearray_reduce_ex_impl(self, proto);
1072
1073exit:
1074 return return_value;
1075}
1076
1077PyDoc_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
1086static PyObject *
1087bytearray_sizeof_impl(PyByteArrayObject *self);
1088
1089static PyObject *
1090bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1091{
1092 return bytearray_sizeof_impl(self);
1093}
sweeneydea81849b2020-04-22 17:05:48 -04001094/*[clinic end generated code: output=b2919f76709e48dc input=a9049054013a1b77]*/