blob: 2d7c742007751af71538f6fdf8a2cfc07920b0a8 [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
41PyDoc_STRVAR(bytearray_translate__doc__,
Martin Panter1b6c6da2016-08-27 08:35:02 +000042"translate($self, table, /, delete=b\'\')\n"
43"--\n"
44"\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030045"Return a copy with each character mapped by the given translation table.\n"
46"\n"
47" table\n"
48" Translation table, which must be a bytes object of length 256.\n"
49"\n"
Martin Panter1b6c6da2016-08-27 08:35:02 +000050"All characters occurring in the optional argument delete are removed.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030051"The remaining characters are mapped through the given translation table.");
52
53#define BYTEARRAY_TRANSLATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020054 {"translate", (PyCFunction)(void(*)(void))bytearray_translate, METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030055
56static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -040057bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
Martin Panter1b6c6da2016-08-27 08:35:02 +000058 PyObject *deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030059
60static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020061bytearray_translate(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030062{
63 PyObject *return_value = NULL;
Martin Panter1b6c6da2016-08-27 08:35:02 +000064 static const char * const _keywords[] = {"", "delete", NULL};
65 static _PyArg_Parser _parser = {"O|O:translate", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030066 PyObject *table;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030067 PyObject *deletechars = NULL;
68
Victor Stinner3e1fad62017-01-17 01:29:01 +010069 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Martin Panter1b6c6da2016-08-27 08:35:02 +000070 &table, &deletechars)) {
71 goto exit;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030072 }
Martin Panter1b6c6da2016-08-27 08:35:02 +000073 return_value = bytearray_translate_impl(self, table, deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030074
75exit:
76 return return_value;
77}
78
79PyDoc_STRVAR(bytearray_maketrans__doc__,
80"maketrans(frm, to, /)\n"
81"--\n"
82"\n"
83"Return a translation table useable for the bytes or bytearray translate method.\n"
84"\n"
85"The returned table will be one where each byte in frm is mapped to the byte at\n"
86"the same position in to.\n"
87"\n"
88"The bytes objects frm and to must be of the same length.");
89
90#define BYTEARRAY_MAKETRANS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020091 {"maketrans", (PyCFunction)(void(*)(void))bytearray_maketrans, METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030092
93static PyObject *
94bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
95
96static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020097bytearray_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030098{
99 PyObject *return_value = NULL;
100 Py_buffer frm = {NULL, NULL};
101 Py_buffer to = {NULL, NULL};
102
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200103 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
104 goto exit;
105 }
106 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
107 goto exit;
108 }
109 if (!PyBuffer_IsContiguous(&frm, 'C')) {
110 _PyArg_BadArgument("maketrans", 1, "contiguous buffer", args[0]);
111 goto exit;
112 }
113 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
114 goto exit;
115 }
116 if (!PyBuffer_IsContiguous(&to, 'C')) {
117 _PyArg_BadArgument("maketrans", 2, "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100118 goto exit;
119 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300120 return_value = bytearray_maketrans_impl(&frm, &to);
121
122exit:
123 /* Cleanup for frm */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300124 if (frm.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300125 PyBuffer_Release(&frm);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300126 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300127 /* Cleanup for to */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300128 if (to.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300129 PyBuffer_Release(&to);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300130 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300131
132 return return_value;
133}
134
135PyDoc_STRVAR(bytearray_replace__doc__,
136"replace($self, old, new, count=-1, /)\n"
137"--\n"
138"\n"
139"Return a copy with all occurrences of substring old replaced by new.\n"
140"\n"
141" count\n"
142" Maximum number of occurrences to replace.\n"
143" -1 (the default value) means replace all occurrences.\n"
144"\n"
145"If the optional argument count is given, only the first count occurrences are\n"
146"replaced.");
147
148#define BYTEARRAY_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200149 {"replace", (PyCFunction)(void(*)(void))bytearray_replace, METH_FASTCALL, bytearray_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300150
151static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400152bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
153 Py_buffer *new, Py_ssize_t count);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300154
155static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200156bytearray_replace(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300157{
158 PyObject *return_value = NULL;
159 Py_buffer old = {NULL, NULL};
160 Py_buffer new = {NULL, NULL};
161 Py_ssize_t count = -1;
162
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200163 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100164 goto exit;
165 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200166 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
167 goto exit;
168 }
169 if (!PyBuffer_IsContiguous(&old, 'C')) {
170 _PyArg_BadArgument("replace", 1, "contiguous buffer", args[0]);
171 goto exit;
172 }
173 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
174 goto exit;
175 }
176 if (!PyBuffer_IsContiguous(&new, 'C')) {
177 _PyArg_BadArgument("replace", 2, "contiguous buffer", args[1]);
178 goto exit;
179 }
180 if (nargs < 3) {
181 goto skip_optional;
182 }
183 if (PyFloat_Check(args[2])) {
184 PyErr_SetString(PyExc_TypeError,
185 "integer argument expected, got float" );
186 goto exit;
187 }
188 {
189 Py_ssize_t ival = -1;
190 PyObject *iobj = PyNumber_Index(args[2]);
191 if (iobj != NULL) {
192 ival = PyLong_AsSsize_t(iobj);
193 Py_DECREF(iobj);
194 }
195 if (ival == -1 && PyErr_Occurred()) {
196 goto exit;
197 }
198 count = ival;
199 }
200skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300201 return_value = bytearray_replace_impl(self, &old, &new, count);
202
203exit:
204 /* Cleanup for old */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300205 if (old.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300206 PyBuffer_Release(&old);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300207 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300208 /* Cleanup for new */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300209 if (new.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300210 PyBuffer_Release(&new);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300211 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300212
213 return return_value;
214}
215
216PyDoc_STRVAR(bytearray_split__doc__,
217"split($self, /, sep=None, maxsplit=-1)\n"
218"--\n"
219"\n"
220"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
221"\n"
222" sep\n"
223" The delimiter according which to split the bytearray.\n"
224" None (the default value) means split on ASCII whitespace characters\n"
225" (space, tab, return, newline, formfeed, vertical tab).\n"
226" maxsplit\n"
227" Maximum number of splits to do.\n"
228" -1 (the default value) means no limit.");
229
230#define BYTEARRAY_SPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200231 {"split", (PyCFunction)(void(*)(void))bytearray_split, METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300232
233static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400234bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
235 Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300236
237static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200238bytearray_split(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300239{
240 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300241 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
242 static _PyArg_Parser _parser = {"|On:split", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300243 PyObject *sep = Py_None;
244 Py_ssize_t maxsplit = -1;
245
Victor Stinner3e1fad62017-01-17 01:29:01 +0100246 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300247 &sep, &maxsplit)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300248 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300249 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300250 return_value = bytearray_split_impl(self, sep, maxsplit);
251
252exit:
253 return return_value;
254}
255
256PyDoc_STRVAR(bytearray_partition__doc__,
257"partition($self, sep, /)\n"
258"--\n"
259"\n"
260"Partition the bytearray into three parts using the given separator.\n"
261"\n"
262"This will search for the separator sep in the bytearray. If the separator is\n"
263"found, returns a 3-tuple containing the part before the separator, the\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300264"separator itself, and the part after it as new bytearray objects.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300265"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300266"If the separator is not found, returns a 3-tuple containing the copy of the\n"
267"original bytearray object and two empty bytearray objects.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300268
269#define BYTEARRAY_PARTITION_METHODDEF \
270 {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
271
272PyDoc_STRVAR(bytearray_rpartition__doc__,
273"rpartition($self, sep, /)\n"
274"--\n"
275"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300276"Partition the bytearray into three parts using the given separator.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300277"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300278"This will search for the separator sep in the bytearray, starting at the end.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300279"If the separator is found, returns a 3-tuple containing the part before the\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300280"separator, the separator itself, and the part after it as new bytearray\n"
281"objects.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300282"\n"
283"If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300284"objects and the copy of the original bytearray object.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300285
286#define BYTEARRAY_RPARTITION_METHODDEF \
287 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
288
289PyDoc_STRVAR(bytearray_rsplit__doc__,
290"rsplit($self, /, sep=None, maxsplit=-1)\n"
291"--\n"
292"\n"
293"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
294"\n"
295" sep\n"
296" The delimiter according which to split the bytearray.\n"
297" None (the default value) means split on ASCII whitespace characters\n"
298" (space, tab, return, newline, formfeed, vertical tab).\n"
299" maxsplit\n"
300" Maximum number of splits to do.\n"
301" -1 (the default value) means no limit.\n"
302"\n"
303"Splitting is done starting at the end of the bytearray and working to the front.");
304
305#define BYTEARRAY_RSPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200306 {"rsplit", (PyCFunction)(void(*)(void))bytearray_rsplit, METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300307
308static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400309bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
310 Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300311
312static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200313bytearray_rsplit(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300314{
315 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300316 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
317 static _PyArg_Parser _parser = {"|On:rsplit", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300318 PyObject *sep = Py_None;
319 Py_ssize_t maxsplit = -1;
320
Victor Stinner3e1fad62017-01-17 01:29:01 +0100321 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300322 &sep, &maxsplit)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300323 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300324 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300325 return_value = bytearray_rsplit_impl(self, sep, maxsplit);
326
327exit:
328 return return_value;
329}
330
331PyDoc_STRVAR(bytearray_reverse__doc__,
332"reverse($self, /)\n"
333"--\n"
334"\n"
335"Reverse the order of the values in B in place.");
336
337#define BYTEARRAY_REVERSE_METHODDEF \
338 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
339
340static PyObject *
341bytearray_reverse_impl(PyByteArrayObject *self);
342
343static PyObject *
344bytearray_reverse(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
345{
346 return bytearray_reverse_impl(self);
347}
348
349PyDoc_STRVAR(bytearray_insert__doc__,
350"insert($self, index, item, /)\n"
351"--\n"
352"\n"
353"Insert a single item into the bytearray before the given index.\n"
354"\n"
355" index\n"
356" The index where the value is to be inserted.\n"
357" item\n"
358" The item to be inserted.");
359
360#define BYTEARRAY_INSERT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200361 {"insert", (PyCFunction)(void(*)(void))bytearray_insert, METH_FASTCALL, bytearray_insert__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300362
363static PyObject *
364bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
365
366static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200367bytearray_insert(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300368{
369 PyObject *return_value = NULL;
370 Py_ssize_t index;
371 int item;
372
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200373 if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
374 goto exit;
375 }
376 if (PyFloat_Check(args[0])) {
377 PyErr_SetString(PyExc_TypeError,
378 "integer argument expected, got float" );
379 goto exit;
380 }
381 {
382 Py_ssize_t ival = -1;
383 PyObject *iobj = PyNumber_Index(args[0]);
384 if (iobj != NULL) {
385 ival = PyLong_AsSsize_t(iobj);
386 Py_DECREF(iobj);
387 }
388 if (ival == -1 && PyErr_Occurred()) {
389 goto exit;
390 }
391 index = ival;
392 }
393 if (!_getbytevalue(args[1], &item)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100394 goto exit;
395 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300396 return_value = bytearray_insert_impl(self, index, item);
397
398exit:
399 return return_value;
400}
401
402PyDoc_STRVAR(bytearray_append__doc__,
403"append($self, item, /)\n"
404"--\n"
405"\n"
406"Append a single item to the end of the bytearray.\n"
407"\n"
408" item\n"
409" The item to be appended.");
410
411#define BYTEARRAY_APPEND_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300412 {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300413
414static PyObject *
415bytearray_append_impl(PyByteArrayObject *self, int item);
416
417static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300418bytearray_append(PyByteArrayObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300419{
420 PyObject *return_value = NULL;
421 int item;
422
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200423 if (!_getbytevalue(arg, &item)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300424 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300425 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300426 return_value = bytearray_append_impl(self, item);
427
428exit:
429 return return_value;
430}
431
432PyDoc_STRVAR(bytearray_extend__doc__,
433"extend($self, iterable_of_ints, /)\n"
434"--\n"
435"\n"
436"Append all the items from the iterator or sequence to the end of the bytearray.\n"
437"\n"
438" iterable_of_ints\n"
439" The iterable of items to append.");
440
441#define BYTEARRAY_EXTEND_METHODDEF \
442 {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
443
444PyDoc_STRVAR(bytearray_pop__doc__,
445"pop($self, index=-1, /)\n"
446"--\n"
447"\n"
448"Remove and return a single item from B.\n"
449"\n"
450" index\n"
451" The index from where to remove the item.\n"
452" -1 (the default value) means remove the last item.\n"
453"\n"
454"If no index argument is given, will pop the last item.");
455
456#define BYTEARRAY_POP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200457 {"pop", (PyCFunction)(void(*)(void))bytearray_pop, METH_FASTCALL, bytearray_pop__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300458
459static PyObject *
460bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
461
462static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200463bytearray_pop(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300464{
465 PyObject *return_value = NULL;
466 Py_ssize_t index = -1;
467
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200468 if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100469 goto exit;
470 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200471 if (nargs < 1) {
472 goto skip_optional;
473 }
474 if (PyFloat_Check(args[0])) {
475 PyErr_SetString(PyExc_TypeError,
476 "integer argument expected, got float" );
477 goto exit;
478 }
479 {
480 Py_ssize_t ival = -1;
481 PyObject *iobj = PyNumber_Index(args[0]);
482 if (iobj != NULL) {
483 ival = PyLong_AsSsize_t(iobj);
484 Py_DECREF(iobj);
485 }
486 if (ival == -1 && PyErr_Occurred()) {
487 goto exit;
488 }
489 index = ival;
490 }
491skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300492 return_value = bytearray_pop_impl(self, index);
493
494exit:
495 return return_value;
496}
497
498PyDoc_STRVAR(bytearray_remove__doc__,
499"remove($self, value, /)\n"
500"--\n"
501"\n"
502"Remove the first occurrence of a value in the bytearray.\n"
503"\n"
504" value\n"
505" The value to remove.");
506
507#define BYTEARRAY_REMOVE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300508 {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300509
510static PyObject *
511bytearray_remove_impl(PyByteArrayObject *self, int value);
512
513static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300514bytearray_remove(PyByteArrayObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300515{
516 PyObject *return_value = NULL;
517 int value;
518
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200519 if (!_getbytevalue(arg, &value)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300520 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300521 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300522 return_value = bytearray_remove_impl(self, value);
523
524exit:
525 return return_value;
526}
527
528PyDoc_STRVAR(bytearray_strip__doc__,
529"strip($self, bytes=None, /)\n"
530"--\n"
531"\n"
532"Strip leading and trailing bytes contained in the argument.\n"
533"\n"
534"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
535
536#define BYTEARRAY_STRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200537 {"strip", (PyCFunction)(void(*)(void))bytearray_strip, METH_FASTCALL, bytearray_strip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300538
539static PyObject *
540bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
541
542static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200543bytearray_strip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300544{
545 PyObject *return_value = NULL;
546 PyObject *bytes = Py_None;
547
Sylvain74453812017-06-10 06:51:48 +0200548 if (!_PyArg_UnpackStack(args, nargs, "strip",
549 0, 1,
550 &bytes)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100551 goto exit;
552 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300553 return_value = bytearray_strip_impl(self, bytes);
554
555exit:
556 return return_value;
557}
558
559PyDoc_STRVAR(bytearray_lstrip__doc__,
560"lstrip($self, bytes=None, /)\n"
561"--\n"
562"\n"
563"Strip leading bytes contained in the argument.\n"
564"\n"
565"If the argument is omitted or None, strip leading ASCII whitespace.");
566
567#define BYTEARRAY_LSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200568 {"lstrip", (PyCFunction)(void(*)(void))bytearray_lstrip, METH_FASTCALL, bytearray_lstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300569
570static PyObject *
571bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
572
573static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200574bytearray_lstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300575{
576 PyObject *return_value = NULL;
577 PyObject *bytes = Py_None;
578
Sylvain74453812017-06-10 06:51:48 +0200579 if (!_PyArg_UnpackStack(args, nargs, "lstrip",
580 0, 1,
581 &bytes)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100582 goto exit;
583 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300584 return_value = bytearray_lstrip_impl(self, bytes);
585
586exit:
587 return return_value;
588}
589
590PyDoc_STRVAR(bytearray_rstrip__doc__,
591"rstrip($self, bytes=None, /)\n"
592"--\n"
593"\n"
594"Strip trailing bytes contained in the argument.\n"
595"\n"
596"If the argument is omitted or None, strip trailing ASCII whitespace.");
597
598#define BYTEARRAY_RSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200599 {"rstrip", (PyCFunction)(void(*)(void))bytearray_rstrip, METH_FASTCALL, bytearray_rstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300600
601static PyObject *
602bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
603
604static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200605bytearray_rstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300606{
607 PyObject *return_value = NULL;
608 PyObject *bytes = Py_None;
609
Sylvain74453812017-06-10 06:51:48 +0200610 if (!_PyArg_UnpackStack(args, nargs, "rstrip",
611 0, 1,
612 &bytes)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100613 goto exit;
614 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300615 return_value = bytearray_rstrip_impl(self, bytes);
616
617exit:
618 return return_value;
619}
620
621PyDoc_STRVAR(bytearray_decode__doc__,
622"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
623"--\n"
624"\n"
625"Decode the bytearray using the codec registered for encoding.\n"
626"\n"
627" encoding\n"
628" The encoding with which to decode the bytearray.\n"
629" errors\n"
630" The error handling scheme to use for the handling of decoding errors.\n"
631" The default is \'strict\' meaning that decoding errors raise a\n"
632" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
633" as well as any other name registered with codecs.register_error that\n"
634" can handle UnicodeDecodeErrors.");
635
636#define BYTEARRAY_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200637 {"decode", (PyCFunction)(void(*)(void))bytearray_decode, METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300638
639static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400640bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
641 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300642
643static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200644bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300645{
646 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300647 static const char * const _keywords[] = {"encoding", "errors", NULL};
648 static _PyArg_Parser _parser = {"|ss:decode", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300649 const char *encoding = NULL;
650 const char *errors = NULL;
651
Victor Stinner3e1fad62017-01-17 01:29:01 +0100652 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300653 &encoding, &errors)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300654 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300655 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300656 return_value = bytearray_decode_impl(self, encoding, errors);
657
658exit:
659 return return_value;
660}
661
662PyDoc_STRVAR(bytearray_join__doc__,
663"join($self, iterable_of_bytes, /)\n"
664"--\n"
665"\n"
666"Concatenate any number of bytes/bytearray objects.\n"
667"\n"
668"The bytearray whose method is called is inserted in between each pair.\n"
669"\n"
670"The result is returned as a new bytearray object.");
671
672#define BYTEARRAY_JOIN_METHODDEF \
673 {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
674
675PyDoc_STRVAR(bytearray_splitlines__doc__,
676"splitlines($self, /, keepends=False)\n"
677"--\n"
678"\n"
679"Return a list of the lines in the bytearray, breaking at line boundaries.\n"
680"\n"
681"Line breaks are not included in the resulting list unless keepends is given and\n"
682"true.");
683
684#define BYTEARRAY_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200685 {"splitlines", (PyCFunction)(void(*)(void))bytearray_splitlines, METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300686
687static PyObject *
688bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
689
690static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200691bytearray_splitlines(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300692{
693 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300694 static const char * const _keywords[] = {"keepends", NULL};
695 static _PyArg_Parser _parser = {"|i:splitlines", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300696 int keepends = 0;
697
Victor Stinner3e1fad62017-01-17 01:29:01 +0100698 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300699 &keepends)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300700 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300701 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300702 return_value = bytearray_splitlines_impl(self, keepends);
703
704exit:
705 return return_value;
706}
707
708PyDoc_STRVAR(bytearray_fromhex__doc__,
709"fromhex($type, string, /)\n"
710"--\n"
711"\n"
712"Create a bytearray object from a string of hexadecimal numbers.\n"
713"\n"
714"Spaces between two numbers are accepted.\n"
715"Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
716
717#define BYTEARRAY_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300718 {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300719
720static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300721bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300722
723static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300724bytearray_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300725{
726 PyObject *return_value = NULL;
727 PyObject *string;
728
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200729 if (!PyUnicode_Check(arg)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200730 _PyArg_BadArgument("fromhex", 0, "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300731 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300732 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200733 if (PyUnicode_READY(arg) == -1) {
734 goto exit;
735 }
736 string = arg;
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300737 return_value = bytearray_fromhex_impl(type, string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300738
739exit:
740 return return_value;
741}
742
743PyDoc_STRVAR(bytearray_reduce__doc__,
744"__reduce__($self, /)\n"
745"--\n"
746"\n"
747"Return state information for pickling.");
748
749#define BYTEARRAY_REDUCE_METHODDEF \
750 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
751
752static PyObject *
753bytearray_reduce_impl(PyByteArrayObject *self);
754
755static PyObject *
756bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
757{
758 return bytearray_reduce_impl(self);
759}
760
761PyDoc_STRVAR(bytearray_reduce_ex__doc__,
762"__reduce_ex__($self, proto=0, /)\n"
763"--\n"
764"\n"
765"Return state information for pickling.");
766
767#define BYTEARRAY_REDUCE_EX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200768 {"__reduce_ex__", (PyCFunction)(void(*)(void))bytearray_reduce_ex, METH_FASTCALL, bytearray_reduce_ex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300769
770static PyObject *
771bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
772
773static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200774bytearray_reduce_ex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300775{
776 PyObject *return_value = NULL;
777 int proto = 0;
778
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200779 if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100780 goto exit;
781 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200782 if (nargs < 1) {
783 goto skip_optional;
784 }
785 if (PyFloat_Check(args[0])) {
786 PyErr_SetString(PyExc_TypeError,
787 "integer argument expected, got float" );
788 goto exit;
789 }
790 proto = _PyLong_AsInt(args[0]);
791 if (proto == -1 && PyErr_Occurred()) {
792 goto exit;
793 }
794skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300795 return_value = bytearray_reduce_ex_impl(self, proto);
796
797exit:
798 return return_value;
799}
800
801PyDoc_STRVAR(bytearray_sizeof__doc__,
802"__sizeof__($self, /)\n"
803"--\n"
804"\n"
805"Returns the size of the bytearray object in memory, in bytes.");
806
807#define BYTEARRAY_SIZEOF_METHODDEF \
808 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
809
810static PyObject *
811bytearray_sizeof_impl(PyByteArrayObject *self);
812
813static PyObject *
814bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
815{
816 return bytearray_sizeof_impl(self);
817}
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200818/*[clinic end generated code: output=010e281b823d7df1 input=a9049054013a1b77]*/