blob: 1e3f197561523fac4cb1cbff6f81da9947f738dd [file] [log] [blame]
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
Serhiy Storchaka12f43342020-07-20 15:53:55 +03005static int
6bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
7 const char *encoding, const char *errors);
8
9static int
10bytearray___init__(PyObject *self, PyObject *args, PyObject *kwargs)
11{
12 int return_value = -1;
13 static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
14 static _PyArg_Parser _parser = {NULL, _keywords, "bytearray", 0};
15 PyObject *argsbuf[3];
16 PyObject * const *fastargs;
17 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
18 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
19 PyObject *arg = NULL;
20 const char *encoding = NULL;
21 const char *errors = NULL;
22
23 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
24 if (!fastargs) {
25 goto exit;
26 }
27 if (!noptargs) {
28 goto skip_optional_pos;
29 }
30 if (fastargs[0]) {
31 arg = fastargs[0];
32 if (!--noptargs) {
33 goto skip_optional_pos;
34 }
35 }
36 if (fastargs[1]) {
37 if (!PyUnicode_Check(fastargs[1])) {
38 _PyArg_BadArgument("bytearray", "argument 'encoding'", "str", fastargs[1]);
39 goto exit;
40 }
41 Py_ssize_t encoding_length;
42 encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
43 if (encoding == NULL) {
44 goto exit;
45 }
46 if (strlen(encoding) != (size_t)encoding_length) {
47 PyErr_SetString(PyExc_ValueError, "embedded null character");
48 goto exit;
49 }
50 if (!--noptargs) {
51 goto skip_optional_pos;
52 }
53 }
54 if (!PyUnicode_Check(fastargs[2])) {
55 _PyArg_BadArgument("bytearray", "argument 'errors'", "str", fastargs[2]);
56 goto exit;
57 }
58 Py_ssize_t errors_length;
59 errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
60 if (errors == NULL) {
61 goto exit;
62 }
63 if (strlen(errors) != (size_t)errors_length) {
64 PyErr_SetString(PyExc_ValueError, "embedded null character");
65 goto exit;
66 }
67skip_optional_pos:
68 return_value = bytearray___init___impl((PyByteArrayObject *)self, arg, encoding, errors);
69
70exit:
71 return return_value;
72}
73
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030074PyDoc_STRVAR(bytearray_clear__doc__,
75"clear($self, /)\n"
76"--\n"
77"\n"
78"Remove all items from the bytearray.");
79
80#define BYTEARRAY_CLEAR_METHODDEF \
81 {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, bytearray_clear__doc__},
82
83static PyObject *
84bytearray_clear_impl(PyByteArrayObject *self);
85
86static PyObject *
87bytearray_clear(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
88{
89 return bytearray_clear_impl(self);
90}
91
92PyDoc_STRVAR(bytearray_copy__doc__,
93"copy($self, /)\n"
94"--\n"
95"\n"
96"Return a copy of B.");
97
98#define BYTEARRAY_COPY_METHODDEF \
99 {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, bytearray_copy__doc__},
100
101static PyObject *
102bytearray_copy_impl(PyByteArrayObject *self);
103
104static PyObject *
105bytearray_copy(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
106{
107 return bytearray_copy_impl(self);
108}
109
sweeneydea81849b2020-04-22 17:05:48 -0400110PyDoc_STRVAR(bytearray_removeprefix__doc__,
111"removeprefix($self, prefix, /)\n"
112"--\n"
113"\n"
114"Return a bytearray with the given prefix string removed if present.\n"
115"\n"
116"If the bytearray starts with the prefix string, return\n"
117"bytearray[len(prefix):]. Otherwise, return a copy of the original\n"
118"bytearray.");
119
120#define BYTEARRAY_REMOVEPREFIX_METHODDEF \
121 {"removeprefix", (PyCFunction)bytearray_removeprefix, METH_O, bytearray_removeprefix__doc__},
122
123static PyObject *
124bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix);
125
126static PyObject *
127bytearray_removeprefix(PyByteArrayObject *self, PyObject *arg)
128{
129 PyObject *return_value = NULL;
130 Py_buffer prefix = {NULL, NULL};
131
132 if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
133 goto exit;
134 }
135 if (!PyBuffer_IsContiguous(&prefix, 'C')) {
136 _PyArg_BadArgument("removeprefix", "argument", "contiguous buffer", arg);
137 goto exit;
138 }
139 return_value = bytearray_removeprefix_impl(self, &prefix);
140
141exit:
142 /* Cleanup for prefix */
143 if (prefix.obj) {
144 PyBuffer_Release(&prefix);
145 }
146
147 return return_value;
148}
149
150PyDoc_STRVAR(bytearray_removesuffix__doc__,
151"removesuffix($self, suffix, /)\n"
152"--\n"
153"\n"
154"Return a bytearray with the given suffix string removed if present.\n"
155"\n"
156"If the bytearray ends with the suffix string and that suffix is not\n"
157"empty, return bytearray[:-len(suffix)]. Otherwise, return a copy of\n"
158"the original bytearray.");
159
160#define BYTEARRAY_REMOVESUFFIX_METHODDEF \
161 {"removesuffix", (PyCFunction)bytearray_removesuffix, METH_O, bytearray_removesuffix__doc__},
162
163static PyObject *
164bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix);
165
166static PyObject *
167bytearray_removesuffix(PyByteArrayObject *self, PyObject *arg)
168{
169 PyObject *return_value = NULL;
170 Py_buffer suffix = {NULL, NULL};
171
172 if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
173 goto exit;
174 }
175 if (!PyBuffer_IsContiguous(&suffix, 'C')) {
176 _PyArg_BadArgument("removesuffix", "argument", "contiguous buffer", arg);
177 goto exit;
178 }
179 return_value = bytearray_removesuffix_impl(self, &suffix);
180
181exit:
182 /* Cleanup for suffix */
183 if (suffix.obj) {
184 PyBuffer_Release(&suffix);
185 }
186
187 return return_value;
188}
189
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300190PyDoc_STRVAR(bytearray_translate__doc__,
Martin Panter1b6c6da2016-08-27 08:35:02 +0000191"translate($self, table, /, delete=b\'\')\n"
192"--\n"
193"\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300194"Return a copy with each character mapped by the given translation table.\n"
195"\n"
196" table\n"
197" Translation table, which must be a bytes object of length 256.\n"
198"\n"
Martin Panter1b6c6da2016-08-27 08:35:02 +0000199"All characters occurring in the optional argument delete are removed.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300200"The remaining characters are mapped through the given translation table.");
201
202#define BYTEARRAY_TRANSLATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200203 {"translate", (PyCFunction)(void(*)(void))bytearray_translate, METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300204
205static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400206bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +0000207 PyObject *deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300208
209static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200210bytearray_translate(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300211{
212 PyObject *return_value = NULL;
Martin Panter1b6c6da2016-08-27 08:35:02 +0000213 static const char * const _keywords[] = {"", "delete", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200214 static _PyArg_Parser _parser = {NULL, _keywords, "translate", 0};
215 PyObject *argsbuf[2];
216 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300217 PyObject *table;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300218 PyObject *deletechars = NULL;
219
Serhiy Storchaka31913912019-03-14 10:32:22 +0200220 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
221 if (!args) {
Martin Panter1b6c6da2016-08-27 08:35:02 +0000222 goto exit;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300223 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200224 table = args[0];
225 if (!noptargs) {
226 goto skip_optional_pos;
227 }
228 deletechars = args[1];
229skip_optional_pos:
Martin Panter1b6c6da2016-08-27 08:35:02 +0000230 return_value = bytearray_translate_impl(self, table, deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300231
232exit:
233 return return_value;
234}
235
236PyDoc_STRVAR(bytearray_maketrans__doc__,
237"maketrans(frm, to, /)\n"
238"--\n"
239"\n"
240"Return a translation table useable for the bytes or bytearray translate method.\n"
241"\n"
242"The returned table will be one where each byte in frm is mapped to the byte at\n"
243"the same position in to.\n"
244"\n"
245"The bytes objects frm and to must be of the same length.");
246
247#define BYTEARRAY_MAKETRANS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200248 {"maketrans", (PyCFunction)(void(*)(void))bytearray_maketrans, METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300249
250static PyObject *
251bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
252
253static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200254bytearray_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300255{
256 PyObject *return_value = NULL;
257 Py_buffer frm = {NULL, NULL};
258 Py_buffer to = {NULL, NULL};
259
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200260 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
261 goto exit;
262 }
263 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
264 goto exit;
265 }
266 if (!PyBuffer_IsContiguous(&frm, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200267 _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200268 goto exit;
269 }
270 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
271 goto exit;
272 }
273 if (!PyBuffer_IsContiguous(&to, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200274 _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100275 goto exit;
276 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300277 return_value = bytearray_maketrans_impl(&frm, &to);
278
279exit:
280 /* Cleanup for frm */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300281 if (frm.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300282 PyBuffer_Release(&frm);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300283 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300284 /* Cleanup for to */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300285 if (to.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300286 PyBuffer_Release(&to);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300287 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300288
289 return return_value;
290}
291
292PyDoc_STRVAR(bytearray_replace__doc__,
293"replace($self, old, new, count=-1, /)\n"
294"--\n"
295"\n"
296"Return a copy with all occurrences of substring old replaced by new.\n"
297"\n"
298" count\n"
299" Maximum number of occurrences to replace.\n"
300" -1 (the default value) means replace all occurrences.\n"
301"\n"
302"If the optional argument count is given, only the first count occurrences are\n"
303"replaced.");
304
305#define BYTEARRAY_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200306 {"replace", (PyCFunction)(void(*)(void))bytearray_replace, METH_FASTCALL, bytearray_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300307
308static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400309bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
310 Py_buffer *new, Py_ssize_t count);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300311
312static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200313bytearray_replace(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300314{
315 PyObject *return_value = NULL;
316 Py_buffer old = {NULL, NULL};
317 Py_buffer new = {NULL, NULL};
318 Py_ssize_t count = -1;
319
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200320 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100321 goto exit;
322 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200323 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
324 goto exit;
325 }
326 if (!PyBuffer_IsContiguous(&old, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200327 _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200328 goto exit;
329 }
330 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
331 goto exit;
332 }
333 if (!PyBuffer_IsContiguous(&new, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200334 _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200335 goto exit;
336 }
337 if (nargs < 3) {
338 goto skip_optional;
339 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200340 {
341 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300342 PyObject *iobj = _PyNumber_Index(args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200343 if (iobj != NULL) {
344 ival = PyLong_AsSsize_t(iobj);
345 Py_DECREF(iobj);
346 }
347 if (ival == -1 && PyErr_Occurred()) {
348 goto exit;
349 }
350 count = ival;
351 }
352skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300353 return_value = bytearray_replace_impl(self, &old, &new, count);
354
355exit:
356 /* Cleanup for old */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300357 if (old.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300358 PyBuffer_Release(&old);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300359 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300360 /* Cleanup for new */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300361 if (new.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300362 PyBuffer_Release(&new);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300363 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300364
365 return return_value;
366}
367
368PyDoc_STRVAR(bytearray_split__doc__,
369"split($self, /, sep=None, maxsplit=-1)\n"
370"--\n"
371"\n"
372"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
373"\n"
374" sep\n"
375" The delimiter according which to split the bytearray.\n"
376" None (the default value) means split on ASCII whitespace characters\n"
377" (space, tab, return, newline, formfeed, vertical tab).\n"
378" maxsplit\n"
379" Maximum number of splits to do.\n"
380" -1 (the default value) means no limit.");
381
382#define BYTEARRAY_SPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200383 {"split", (PyCFunction)(void(*)(void))bytearray_split, METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300384
385static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400386bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
387 Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300388
389static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200390bytearray_split(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300391{
392 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300393 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200394 static _PyArg_Parser _parser = {NULL, _keywords, "split", 0};
395 PyObject *argsbuf[2];
396 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300397 PyObject *sep = Py_None;
398 Py_ssize_t maxsplit = -1;
399
Serhiy Storchaka31913912019-03-14 10:32:22 +0200400 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
401 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300402 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300403 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200404 if (!noptargs) {
405 goto skip_optional_pos;
406 }
407 if (args[0]) {
408 sep = args[0];
409 if (!--noptargs) {
410 goto skip_optional_pos;
411 }
412 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200413 {
414 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300415 PyObject *iobj = _PyNumber_Index(args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200416 if (iobj != NULL) {
417 ival = PyLong_AsSsize_t(iobj);
418 Py_DECREF(iobj);
419 }
420 if (ival == -1 && PyErr_Occurred()) {
421 goto exit;
422 }
423 maxsplit = ival;
424 }
425skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300426 return_value = bytearray_split_impl(self, sep, maxsplit);
427
428exit:
429 return return_value;
430}
431
432PyDoc_STRVAR(bytearray_partition__doc__,
433"partition($self, sep, /)\n"
434"--\n"
435"\n"
436"Partition the bytearray into three parts using the given separator.\n"
437"\n"
438"This will search for the separator sep in the bytearray. If the separator is\n"
439"found, returns a 3-tuple containing the part before the separator, the\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300440"separator itself, and the part after it as new bytearray objects.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300441"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300442"If the separator is not found, returns a 3-tuple containing the copy of the\n"
443"original bytearray object and two empty bytearray objects.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300444
445#define BYTEARRAY_PARTITION_METHODDEF \
446 {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
447
448PyDoc_STRVAR(bytearray_rpartition__doc__,
449"rpartition($self, sep, /)\n"
450"--\n"
451"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300452"Partition the bytearray into three parts using the given separator.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300453"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300454"This will search for the separator sep in the bytearray, starting at the end.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300455"If the separator is found, returns a 3-tuple containing the part before the\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300456"separator, the separator itself, and the part after it as new bytearray\n"
457"objects.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300458"\n"
459"If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300460"objects and the copy of the original bytearray object.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300461
462#define BYTEARRAY_RPARTITION_METHODDEF \
463 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
464
465PyDoc_STRVAR(bytearray_rsplit__doc__,
466"rsplit($self, /, sep=None, maxsplit=-1)\n"
467"--\n"
468"\n"
469"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
470"\n"
471" sep\n"
472" The delimiter according which to split the bytearray.\n"
473" None (the default value) means split on ASCII whitespace characters\n"
474" (space, tab, return, newline, formfeed, vertical tab).\n"
475" maxsplit\n"
476" Maximum number of splits to do.\n"
477" -1 (the default value) means no limit.\n"
478"\n"
479"Splitting is done starting at the end of the bytearray and working to the front.");
480
481#define BYTEARRAY_RSPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200482 {"rsplit", (PyCFunction)(void(*)(void))bytearray_rsplit, METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300483
484static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400485bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
486 Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300487
488static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200489bytearray_rsplit(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300490{
491 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300492 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200493 static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
494 PyObject *argsbuf[2];
495 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300496 PyObject *sep = Py_None;
497 Py_ssize_t maxsplit = -1;
498
Serhiy Storchaka31913912019-03-14 10:32:22 +0200499 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
500 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300501 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300502 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200503 if (!noptargs) {
504 goto skip_optional_pos;
505 }
506 if (args[0]) {
507 sep = args[0];
508 if (!--noptargs) {
509 goto skip_optional_pos;
510 }
511 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200512 {
513 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300514 PyObject *iobj = _PyNumber_Index(args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200515 if (iobj != NULL) {
516 ival = PyLong_AsSsize_t(iobj);
517 Py_DECREF(iobj);
518 }
519 if (ival == -1 && PyErr_Occurred()) {
520 goto exit;
521 }
522 maxsplit = ival;
523 }
524skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300525 return_value = bytearray_rsplit_impl(self, sep, maxsplit);
526
527exit:
528 return return_value;
529}
530
531PyDoc_STRVAR(bytearray_reverse__doc__,
532"reverse($self, /)\n"
533"--\n"
534"\n"
535"Reverse the order of the values in B in place.");
536
537#define BYTEARRAY_REVERSE_METHODDEF \
538 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
539
540static PyObject *
541bytearray_reverse_impl(PyByteArrayObject *self);
542
543static PyObject *
544bytearray_reverse(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
545{
546 return bytearray_reverse_impl(self);
547}
548
549PyDoc_STRVAR(bytearray_insert__doc__,
550"insert($self, index, item, /)\n"
551"--\n"
552"\n"
553"Insert a single item into the bytearray before the given index.\n"
554"\n"
555" index\n"
556" The index where the value is to be inserted.\n"
557" item\n"
558" The item to be inserted.");
559
560#define BYTEARRAY_INSERT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200561 {"insert", (PyCFunction)(void(*)(void))bytearray_insert, METH_FASTCALL, bytearray_insert__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300562
563static PyObject *
564bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
565
566static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200567bytearray_insert(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300568{
569 PyObject *return_value = NULL;
570 Py_ssize_t index;
571 int item;
572
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200573 if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
574 goto exit;
575 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200576 {
577 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300578 PyObject *iobj = _PyNumber_Index(args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200579 if (iobj != NULL) {
580 ival = PyLong_AsSsize_t(iobj);
581 Py_DECREF(iobj);
582 }
583 if (ival == -1 && PyErr_Occurred()) {
584 goto exit;
585 }
586 index = ival;
587 }
588 if (!_getbytevalue(args[1], &item)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100589 goto exit;
590 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300591 return_value = bytearray_insert_impl(self, index, item);
592
593exit:
594 return return_value;
595}
596
597PyDoc_STRVAR(bytearray_append__doc__,
598"append($self, item, /)\n"
599"--\n"
600"\n"
601"Append a single item to the end of the bytearray.\n"
602"\n"
603" item\n"
604" The item to be appended.");
605
606#define BYTEARRAY_APPEND_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300607 {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300608
609static PyObject *
610bytearray_append_impl(PyByteArrayObject *self, int item);
611
612static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300613bytearray_append(PyByteArrayObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300614{
615 PyObject *return_value = NULL;
616 int item;
617
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200618 if (!_getbytevalue(arg, &item)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300619 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300620 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300621 return_value = bytearray_append_impl(self, item);
622
623exit:
624 return return_value;
625}
626
627PyDoc_STRVAR(bytearray_extend__doc__,
628"extend($self, iterable_of_ints, /)\n"
629"--\n"
630"\n"
631"Append all the items from the iterator or sequence to the end of the bytearray.\n"
632"\n"
633" iterable_of_ints\n"
634" The iterable of items to append.");
635
636#define BYTEARRAY_EXTEND_METHODDEF \
637 {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
638
639PyDoc_STRVAR(bytearray_pop__doc__,
640"pop($self, index=-1, /)\n"
641"--\n"
642"\n"
643"Remove and return a single item from B.\n"
644"\n"
645" index\n"
646" The index from where to remove the item.\n"
647" -1 (the default value) means remove the last item.\n"
648"\n"
649"If no index argument is given, will pop the last item.");
650
651#define BYTEARRAY_POP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200652 {"pop", (PyCFunction)(void(*)(void))bytearray_pop, METH_FASTCALL, bytearray_pop__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300653
654static PyObject *
655bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
656
657static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200658bytearray_pop(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300659{
660 PyObject *return_value = NULL;
661 Py_ssize_t index = -1;
662
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200663 if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100664 goto exit;
665 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200666 if (nargs < 1) {
667 goto skip_optional;
668 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200669 {
670 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300671 PyObject *iobj = _PyNumber_Index(args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200672 if (iobj != NULL) {
673 ival = PyLong_AsSsize_t(iobj);
674 Py_DECREF(iobj);
675 }
676 if (ival == -1 && PyErr_Occurred()) {
677 goto exit;
678 }
679 index = ival;
680 }
681skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300682 return_value = bytearray_pop_impl(self, index);
683
684exit:
685 return return_value;
686}
687
688PyDoc_STRVAR(bytearray_remove__doc__,
689"remove($self, value, /)\n"
690"--\n"
691"\n"
692"Remove the first occurrence of a value in the bytearray.\n"
693"\n"
694" value\n"
695" The value to remove.");
696
697#define BYTEARRAY_REMOVE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300698 {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300699
700static PyObject *
701bytearray_remove_impl(PyByteArrayObject *self, int value);
702
703static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300704bytearray_remove(PyByteArrayObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300705{
706 PyObject *return_value = NULL;
707 int value;
708
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200709 if (!_getbytevalue(arg, &value)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300710 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300711 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300712 return_value = bytearray_remove_impl(self, value);
713
714exit:
715 return return_value;
716}
717
718PyDoc_STRVAR(bytearray_strip__doc__,
719"strip($self, bytes=None, /)\n"
720"--\n"
721"\n"
722"Strip leading and trailing bytes contained in the argument.\n"
723"\n"
724"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
725
726#define BYTEARRAY_STRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200727 {"strip", (PyCFunction)(void(*)(void))bytearray_strip, METH_FASTCALL, bytearray_strip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300728
729static PyObject *
730bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
731
732static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200733bytearray_strip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300734{
735 PyObject *return_value = NULL;
736 PyObject *bytes = Py_None;
737
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200738 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100739 goto exit;
740 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200741 if (nargs < 1) {
742 goto skip_optional;
743 }
744 bytes = args[0];
745skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300746 return_value = bytearray_strip_impl(self, bytes);
747
748exit:
749 return return_value;
750}
751
752PyDoc_STRVAR(bytearray_lstrip__doc__,
753"lstrip($self, bytes=None, /)\n"
754"--\n"
755"\n"
756"Strip leading bytes contained in the argument.\n"
757"\n"
758"If the argument is omitted or None, strip leading ASCII whitespace.");
759
760#define BYTEARRAY_LSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200761 {"lstrip", (PyCFunction)(void(*)(void))bytearray_lstrip, METH_FASTCALL, bytearray_lstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300762
763static PyObject *
764bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
765
766static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200767bytearray_lstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300768{
769 PyObject *return_value = NULL;
770 PyObject *bytes = Py_None;
771
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200772 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100773 goto exit;
774 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200775 if (nargs < 1) {
776 goto skip_optional;
777 }
778 bytes = args[0];
779skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300780 return_value = bytearray_lstrip_impl(self, bytes);
781
782exit:
783 return return_value;
784}
785
786PyDoc_STRVAR(bytearray_rstrip__doc__,
787"rstrip($self, bytes=None, /)\n"
788"--\n"
789"\n"
790"Strip trailing bytes contained in the argument.\n"
791"\n"
792"If the argument is omitted or None, strip trailing ASCII whitespace.");
793
794#define BYTEARRAY_RSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200795 {"rstrip", (PyCFunction)(void(*)(void))bytearray_rstrip, METH_FASTCALL, bytearray_rstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300796
797static PyObject *
798bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
799
800static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200801bytearray_rstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300802{
803 PyObject *return_value = NULL;
804 PyObject *bytes = Py_None;
805
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200806 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100807 goto exit;
808 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200809 if (nargs < 1) {
810 goto skip_optional;
811 }
812 bytes = args[0];
813skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300814 return_value = bytearray_rstrip_impl(self, bytes);
815
816exit:
817 return return_value;
818}
819
820PyDoc_STRVAR(bytearray_decode__doc__,
821"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
822"--\n"
823"\n"
824"Decode the bytearray using the codec registered for encoding.\n"
825"\n"
826" encoding\n"
827" The encoding with which to decode the bytearray.\n"
828" errors\n"
829" The error handling scheme to use for the handling of decoding errors.\n"
830" The default is \'strict\' meaning that decoding errors raise a\n"
831" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
832" as well as any other name registered with codecs.register_error that\n"
833" can handle UnicodeDecodeErrors.");
834
835#define BYTEARRAY_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200836 {"decode", (PyCFunction)(void(*)(void))bytearray_decode, METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300837
838static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400839bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
840 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300841
842static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200843bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300844{
845 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300846 static const char * const _keywords[] = {"encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200847 static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0};
848 PyObject *argsbuf[2];
849 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300850 const char *encoding = NULL;
851 const char *errors = NULL;
852
Serhiy Storchaka31913912019-03-14 10:32:22 +0200853 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
854 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300855 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300856 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200857 if (!noptargs) {
858 goto skip_optional_pos;
859 }
860 if (args[0]) {
861 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200862 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200863 goto exit;
864 }
865 Py_ssize_t encoding_length;
866 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
867 if (encoding == NULL) {
868 goto exit;
869 }
870 if (strlen(encoding) != (size_t)encoding_length) {
871 PyErr_SetString(PyExc_ValueError, "embedded null character");
872 goto exit;
873 }
874 if (!--noptargs) {
875 goto skip_optional_pos;
876 }
877 }
878 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200879 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200880 goto exit;
881 }
882 Py_ssize_t errors_length;
883 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
884 if (errors == NULL) {
885 goto exit;
886 }
887 if (strlen(errors) != (size_t)errors_length) {
888 PyErr_SetString(PyExc_ValueError, "embedded null character");
889 goto exit;
890 }
891skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300892 return_value = bytearray_decode_impl(self, encoding, errors);
893
894exit:
895 return return_value;
896}
897
898PyDoc_STRVAR(bytearray_join__doc__,
899"join($self, iterable_of_bytes, /)\n"
900"--\n"
901"\n"
902"Concatenate any number of bytes/bytearray objects.\n"
903"\n"
904"The bytearray whose method is called is inserted in between each pair.\n"
905"\n"
906"The result is returned as a new bytearray object.");
907
908#define BYTEARRAY_JOIN_METHODDEF \
909 {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
910
911PyDoc_STRVAR(bytearray_splitlines__doc__,
912"splitlines($self, /, keepends=False)\n"
913"--\n"
914"\n"
915"Return a list of the lines in the bytearray, breaking at line boundaries.\n"
916"\n"
917"Line breaks are not included in the resulting list unless keepends is given and\n"
918"true.");
919
920#define BYTEARRAY_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200921 {"splitlines", (PyCFunction)(void(*)(void))bytearray_splitlines, METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300922
923static PyObject *
924bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
925
926static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200927bytearray_splitlines(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300928{
929 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300930 static const char * const _keywords[] = {"keepends", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200931 static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
932 PyObject *argsbuf[1];
933 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300934 int keepends = 0;
935
Serhiy Storchaka31913912019-03-14 10:32:22 +0200936 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
937 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300938 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300939 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200940 if (!noptargs) {
941 goto skip_optional_pos;
942 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200943 keepends = _PyLong_AsInt(args[0]);
944 if (keepends == -1 && PyErr_Occurred()) {
945 goto exit;
946 }
947skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300948 return_value = bytearray_splitlines_impl(self, keepends);
949
950exit:
951 return return_value;
952}
953
954PyDoc_STRVAR(bytearray_fromhex__doc__,
955"fromhex($type, string, /)\n"
956"--\n"
957"\n"
958"Create a bytearray object from a string of hexadecimal numbers.\n"
959"\n"
960"Spaces between two numbers are accepted.\n"
961"Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
962
963#define BYTEARRAY_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300964 {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300965
966static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300967bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300968
969static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300970bytearray_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300971{
972 PyObject *return_value = NULL;
973 PyObject *string;
974
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200975 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200976 _PyArg_BadArgument("fromhex", "argument", "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300977 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300978 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200979 if (PyUnicode_READY(arg) == -1) {
980 goto exit;
981 }
982 string = arg;
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300983 return_value = bytearray_fromhex_impl(type, string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300984
985exit:
986 return return_value;
987}
988
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700989PyDoc_STRVAR(bytearray_hex__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300990"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700991"--\n"
992"\n"
Serhiy Storchaka2ad93822020-12-03 12:46:16 +0200993"Create a string of hexadecimal numbers from a bytearray object.\n"
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700994"\n"
995" sep\n"
996" An optional single character or byte to separate hex bytes.\n"
997" bytes_per_sep\n"
998" How many bytes between separators. Positive values count from the\n"
999" right, negative values count from the left.\n"
1000"\n"
1001"Example:\n"
1002">>> value = bytearray([0xb9, 0x01, 0xef])\n"
1003">>> value.hex()\n"
1004"\'b901ef\'\n"
1005">>> value.hex(\':\')\n"
1006"\'b9:01:ef\'\n"
1007">>> value.hex(\':\', 2)\n"
1008"\'b9:01ef\'\n"
1009">>> value.hex(\':\', -2)\n"
1010"\'b901:ef\'");
1011
1012#define BYTEARRAY_HEX_METHODDEF \
1013 {"hex", (PyCFunction)(void(*)(void))bytearray_hex, METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__},
1014
1015static PyObject *
1016bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep);
1017
1018static PyObject *
1019bytearray_hex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1020{
1021 PyObject *return_value = NULL;
1022 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
1023 static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0};
1024 PyObject *argsbuf[2];
1025 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1026 PyObject *sep = NULL;
1027 int bytes_per_sep = 1;
1028
1029 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
1030 if (!args) {
1031 goto exit;
1032 }
1033 if (!noptargs) {
1034 goto skip_optional_pos;
1035 }
1036 if (args[0]) {
1037 sep = args[0];
1038 if (!--noptargs) {
1039 goto skip_optional_pos;
1040 }
1041 }
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07001042 bytes_per_sep = _PyLong_AsInt(args[1]);
1043 if (bytes_per_sep == -1 && PyErr_Occurred()) {
1044 goto exit;
1045 }
1046skip_optional_pos:
1047 return_value = bytearray_hex_impl(self, sep, bytes_per_sep);
1048
1049exit:
1050 return return_value;
1051}
1052
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001053PyDoc_STRVAR(bytearray_reduce__doc__,
1054"__reduce__($self, /)\n"
1055"--\n"
1056"\n"
1057"Return state information for pickling.");
1058
1059#define BYTEARRAY_REDUCE_METHODDEF \
1060 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
1061
1062static PyObject *
1063bytearray_reduce_impl(PyByteArrayObject *self);
1064
1065static PyObject *
1066bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1067{
1068 return bytearray_reduce_impl(self);
1069}
1070
1071PyDoc_STRVAR(bytearray_reduce_ex__doc__,
1072"__reduce_ex__($self, proto=0, /)\n"
1073"--\n"
1074"\n"
1075"Return state information for pickling.");
1076
1077#define BYTEARRAY_REDUCE_EX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001078 {"__reduce_ex__", (PyCFunction)(void(*)(void))bytearray_reduce_ex, METH_FASTCALL, bytearray_reduce_ex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001079
1080static PyObject *
1081bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
1082
1083static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001084bytearray_reduce_ex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001085{
1086 PyObject *return_value = NULL;
1087 int proto = 0;
1088
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001089 if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001090 goto exit;
1091 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001092 if (nargs < 1) {
1093 goto skip_optional;
1094 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001095 proto = _PyLong_AsInt(args[0]);
1096 if (proto == -1 && PyErr_Occurred()) {
1097 goto exit;
1098 }
1099skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001100 return_value = bytearray_reduce_ex_impl(self, proto);
1101
1102exit:
1103 return return_value;
1104}
1105
1106PyDoc_STRVAR(bytearray_sizeof__doc__,
1107"__sizeof__($self, /)\n"
1108"--\n"
1109"\n"
1110"Returns the size of the bytearray object in memory, in bytes.");
1111
1112#define BYTEARRAY_SIZEOF_METHODDEF \
1113 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
1114
1115static PyObject *
1116bytearray_sizeof_impl(PyByteArrayObject *self);
1117
1118static PyObject *
1119bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1120{
1121 return bytearray_sizeof_impl(self);
1122}
Serhiy Storchaka2ad93822020-12-03 12:46:16 +02001123/*[clinic end generated code: output=a82659f581e55629 input=a9049054013a1b77]*/