blob: a4669f5076f357de5dc1483eb2178f087ee16586 [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
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200548 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100549 goto exit;
550 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200551 if (nargs < 1) {
552 goto skip_optional;
553 }
554 bytes = args[0];
555skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300556 return_value = bytearray_strip_impl(self, bytes);
557
558exit:
559 return return_value;
560}
561
562PyDoc_STRVAR(bytearray_lstrip__doc__,
563"lstrip($self, bytes=None, /)\n"
564"--\n"
565"\n"
566"Strip leading bytes contained in the argument.\n"
567"\n"
568"If the argument is omitted or None, strip leading ASCII whitespace.");
569
570#define BYTEARRAY_LSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200571 {"lstrip", (PyCFunction)(void(*)(void))bytearray_lstrip, METH_FASTCALL, bytearray_lstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300572
573static PyObject *
574bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
575
576static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200577bytearray_lstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300578{
579 PyObject *return_value = NULL;
580 PyObject *bytes = Py_None;
581
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200582 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100583 goto exit;
584 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200585 if (nargs < 1) {
586 goto skip_optional;
587 }
588 bytes = args[0];
589skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300590 return_value = bytearray_lstrip_impl(self, bytes);
591
592exit:
593 return return_value;
594}
595
596PyDoc_STRVAR(bytearray_rstrip__doc__,
597"rstrip($self, bytes=None, /)\n"
598"--\n"
599"\n"
600"Strip trailing bytes contained in the argument.\n"
601"\n"
602"If the argument is omitted or None, strip trailing ASCII whitespace.");
603
604#define BYTEARRAY_RSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200605 {"rstrip", (PyCFunction)(void(*)(void))bytearray_rstrip, METH_FASTCALL, bytearray_rstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300606
607static PyObject *
608bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
609
610static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200611bytearray_rstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300612{
613 PyObject *return_value = NULL;
614 PyObject *bytes = Py_None;
615
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200616 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100617 goto exit;
618 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200619 if (nargs < 1) {
620 goto skip_optional;
621 }
622 bytes = args[0];
623skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300624 return_value = bytearray_rstrip_impl(self, bytes);
625
626exit:
627 return return_value;
628}
629
630PyDoc_STRVAR(bytearray_decode__doc__,
631"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
632"--\n"
633"\n"
634"Decode the bytearray using the codec registered for encoding.\n"
635"\n"
636" encoding\n"
637" The encoding with which to decode the bytearray.\n"
638" errors\n"
639" The error handling scheme to use for the handling of decoding errors.\n"
640" The default is \'strict\' meaning that decoding errors raise a\n"
641" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
642" as well as any other name registered with codecs.register_error that\n"
643" can handle UnicodeDecodeErrors.");
644
645#define BYTEARRAY_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200646 {"decode", (PyCFunction)(void(*)(void))bytearray_decode, METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300647
648static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400649bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
650 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300651
652static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200653bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300654{
655 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300656 static const char * const _keywords[] = {"encoding", "errors", NULL};
657 static _PyArg_Parser _parser = {"|ss:decode", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300658 const char *encoding = NULL;
659 const char *errors = NULL;
660
Victor Stinner3e1fad62017-01-17 01:29:01 +0100661 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300662 &encoding, &errors)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300663 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300664 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300665 return_value = bytearray_decode_impl(self, encoding, errors);
666
667exit:
668 return return_value;
669}
670
671PyDoc_STRVAR(bytearray_join__doc__,
672"join($self, iterable_of_bytes, /)\n"
673"--\n"
674"\n"
675"Concatenate any number of bytes/bytearray objects.\n"
676"\n"
677"The bytearray whose method is called is inserted in between each pair.\n"
678"\n"
679"The result is returned as a new bytearray object.");
680
681#define BYTEARRAY_JOIN_METHODDEF \
682 {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
683
684PyDoc_STRVAR(bytearray_splitlines__doc__,
685"splitlines($self, /, keepends=False)\n"
686"--\n"
687"\n"
688"Return a list of the lines in the bytearray, breaking at line boundaries.\n"
689"\n"
690"Line breaks are not included in the resulting list unless keepends is given and\n"
691"true.");
692
693#define BYTEARRAY_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200694 {"splitlines", (PyCFunction)(void(*)(void))bytearray_splitlines, METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300695
696static PyObject *
697bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
698
699static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200700bytearray_splitlines(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300701{
702 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300703 static const char * const _keywords[] = {"keepends", NULL};
704 static _PyArg_Parser _parser = {"|i:splitlines", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300705 int keepends = 0;
706
Victor Stinner3e1fad62017-01-17 01:29:01 +0100707 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300708 &keepends)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300709 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300710 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300711 return_value = bytearray_splitlines_impl(self, keepends);
712
713exit:
714 return return_value;
715}
716
717PyDoc_STRVAR(bytearray_fromhex__doc__,
718"fromhex($type, string, /)\n"
719"--\n"
720"\n"
721"Create a bytearray object from a string of hexadecimal numbers.\n"
722"\n"
723"Spaces between two numbers are accepted.\n"
724"Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
725
726#define BYTEARRAY_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300727 {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300728
729static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300730bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300731
732static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300733bytearray_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300734{
735 PyObject *return_value = NULL;
736 PyObject *string;
737
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200738 if (!PyUnicode_Check(arg)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200739 _PyArg_BadArgument("fromhex", 0, "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300740 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300741 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200742 if (PyUnicode_READY(arg) == -1) {
743 goto exit;
744 }
745 string = arg;
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300746 return_value = bytearray_fromhex_impl(type, string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300747
748exit:
749 return return_value;
750}
751
752PyDoc_STRVAR(bytearray_reduce__doc__,
753"__reduce__($self, /)\n"
754"--\n"
755"\n"
756"Return state information for pickling.");
757
758#define BYTEARRAY_REDUCE_METHODDEF \
759 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
760
761static PyObject *
762bytearray_reduce_impl(PyByteArrayObject *self);
763
764static PyObject *
765bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
766{
767 return bytearray_reduce_impl(self);
768}
769
770PyDoc_STRVAR(bytearray_reduce_ex__doc__,
771"__reduce_ex__($self, proto=0, /)\n"
772"--\n"
773"\n"
774"Return state information for pickling.");
775
776#define BYTEARRAY_REDUCE_EX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200777 {"__reduce_ex__", (PyCFunction)(void(*)(void))bytearray_reduce_ex, METH_FASTCALL, bytearray_reduce_ex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300778
779static PyObject *
780bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
781
782static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200783bytearray_reduce_ex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300784{
785 PyObject *return_value = NULL;
786 int proto = 0;
787
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200788 if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100789 goto exit;
790 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200791 if (nargs < 1) {
792 goto skip_optional;
793 }
794 if (PyFloat_Check(args[0])) {
795 PyErr_SetString(PyExc_TypeError,
796 "integer argument expected, got float" );
797 goto exit;
798 }
799 proto = _PyLong_AsInt(args[0]);
800 if (proto == -1 && PyErr_Occurred()) {
801 goto exit;
802 }
803skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300804 return_value = bytearray_reduce_ex_impl(self, proto);
805
806exit:
807 return return_value;
808}
809
810PyDoc_STRVAR(bytearray_sizeof__doc__,
811"__sizeof__($self, /)\n"
812"--\n"
813"\n"
814"Returns the size of the bytearray object in memory, in bytes.");
815
816#define BYTEARRAY_SIZEOF_METHODDEF \
817 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
818
819static PyObject *
820bytearray_sizeof_impl(PyByteArrayObject *self);
821
822static PyObject *
823bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
824{
825 return bytearray_sizeof_impl(self);
826}
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200827/*[clinic end generated code: output=f4353c27dcb4a13d input=a9049054013a1b77]*/