blob: 08c6eb53f5878a74fd9a09756bf737f76b69c6e5 [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};
Serhiy Storchaka31913912019-03-14 10:32:22 +020065 static _PyArg_Parser _parser = {NULL, _keywords, "translate", 0};
66 PyObject *argsbuf[2];
67 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030068 PyObject *table;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030069 PyObject *deletechars = NULL;
70
Serhiy Storchaka31913912019-03-14 10:32:22 +020071 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
72 if (!args) {
Martin Panter1b6c6da2016-08-27 08:35:02 +000073 goto exit;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030074 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020075 table = args[0];
76 if (!noptargs) {
77 goto skip_optional_pos;
78 }
79 deletechars = args[1];
80skip_optional_pos:
Martin Panter1b6c6da2016-08-27 08:35:02 +000081 return_value = bytearray_translate_impl(self, table, deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030082
83exit:
84 return return_value;
85}
86
87PyDoc_STRVAR(bytearray_maketrans__doc__,
88"maketrans(frm, to, /)\n"
89"--\n"
90"\n"
91"Return a translation table useable for the bytes or bytearray translate method.\n"
92"\n"
93"The returned table will be one where each byte in frm is mapped to the byte at\n"
94"the same position in to.\n"
95"\n"
96"The bytes objects frm and to must be of the same length.");
97
98#define BYTEARRAY_MAKETRANS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020099 {"maketrans", (PyCFunction)(void(*)(void))bytearray_maketrans, METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300100
101static PyObject *
102bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to);
103
104static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200105bytearray_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300106{
107 PyObject *return_value = NULL;
108 Py_buffer frm = {NULL, NULL};
109 Py_buffer to = {NULL, NULL};
110
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200111 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
112 goto exit;
113 }
114 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
115 goto exit;
116 }
117 if (!PyBuffer_IsContiguous(&frm, 'C')) {
118 _PyArg_BadArgument("maketrans", 1, "contiguous buffer", args[0]);
119 goto exit;
120 }
121 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
122 goto exit;
123 }
124 if (!PyBuffer_IsContiguous(&to, 'C')) {
125 _PyArg_BadArgument("maketrans", 2, "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100126 goto exit;
127 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300128 return_value = bytearray_maketrans_impl(&frm, &to);
129
130exit:
131 /* Cleanup for frm */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300132 if (frm.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300133 PyBuffer_Release(&frm);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300134 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300135 /* Cleanup for to */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300136 if (to.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300137 PyBuffer_Release(&to);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300138 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300139
140 return return_value;
141}
142
143PyDoc_STRVAR(bytearray_replace__doc__,
144"replace($self, old, new, count=-1, /)\n"
145"--\n"
146"\n"
147"Return a copy with all occurrences of substring old replaced by new.\n"
148"\n"
149" count\n"
150" Maximum number of occurrences to replace.\n"
151" -1 (the default value) means replace all occurrences.\n"
152"\n"
153"If the optional argument count is given, only the first count occurrences are\n"
154"replaced.");
155
156#define BYTEARRAY_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200157 {"replace", (PyCFunction)(void(*)(void))bytearray_replace, METH_FASTCALL, bytearray_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300158
159static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400160bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
161 Py_buffer *new, Py_ssize_t count);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300162
163static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200164bytearray_replace(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300165{
166 PyObject *return_value = NULL;
167 Py_buffer old = {NULL, NULL};
168 Py_buffer new = {NULL, NULL};
169 Py_ssize_t count = -1;
170
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200171 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100172 goto exit;
173 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200174 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
175 goto exit;
176 }
177 if (!PyBuffer_IsContiguous(&old, 'C')) {
178 _PyArg_BadArgument("replace", 1, "contiguous buffer", args[0]);
179 goto exit;
180 }
181 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
182 goto exit;
183 }
184 if (!PyBuffer_IsContiguous(&new, 'C')) {
185 _PyArg_BadArgument("replace", 2, "contiguous buffer", args[1]);
186 goto exit;
187 }
188 if (nargs < 3) {
189 goto skip_optional;
190 }
191 if (PyFloat_Check(args[2])) {
192 PyErr_SetString(PyExc_TypeError,
193 "integer argument expected, got float" );
194 goto exit;
195 }
196 {
197 Py_ssize_t ival = -1;
198 PyObject *iobj = PyNumber_Index(args[2]);
199 if (iobj != NULL) {
200 ival = PyLong_AsSsize_t(iobj);
201 Py_DECREF(iobj);
202 }
203 if (ival == -1 && PyErr_Occurred()) {
204 goto exit;
205 }
206 count = ival;
207 }
208skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300209 return_value = bytearray_replace_impl(self, &old, &new, count);
210
211exit:
212 /* Cleanup for old */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300213 if (old.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300214 PyBuffer_Release(&old);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300215 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300216 /* Cleanup for new */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300217 if (new.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300218 PyBuffer_Release(&new);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300219 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300220
221 return return_value;
222}
223
224PyDoc_STRVAR(bytearray_split__doc__,
225"split($self, /, sep=None, maxsplit=-1)\n"
226"--\n"
227"\n"
228"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
229"\n"
230" sep\n"
231" The delimiter according which to split the bytearray.\n"
232" None (the default value) means split on ASCII whitespace characters\n"
233" (space, tab, return, newline, formfeed, vertical tab).\n"
234" maxsplit\n"
235" Maximum number of splits to do.\n"
236" -1 (the default value) means no limit.");
237
238#define BYTEARRAY_SPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200239 {"split", (PyCFunction)(void(*)(void))bytearray_split, METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300240
241static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400242bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
243 Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300244
245static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200246bytearray_split(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300247{
248 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300249 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200250 static _PyArg_Parser _parser = {NULL, _keywords, "split", 0};
251 PyObject *argsbuf[2];
252 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300253 PyObject *sep = Py_None;
254 Py_ssize_t maxsplit = -1;
255
Serhiy Storchaka31913912019-03-14 10:32:22 +0200256 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
257 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300258 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300259 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200260 if (!noptargs) {
261 goto skip_optional_pos;
262 }
263 if (args[0]) {
264 sep = args[0];
265 if (!--noptargs) {
266 goto skip_optional_pos;
267 }
268 }
269 if (PyFloat_Check(args[1])) {
270 PyErr_SetString(PyExc_TypeError,
271 "integer argument expected, got float" );
272 goto exit;
273 }
274 {
275 Py_ssize_t ival = -1;
276 PyObject *iobj = PyNumber_Index(args[1]);
277 if (iobj != NULL) {
278 ival = PyLong_AsSsize_t(iobj);
279 Py_DECREF(iobj);
280 }
281 if (ival == -1 && PyErr_Occurred()) {
282 goto exit;
283 }
284 maxsplit = ival;
285 }
286skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300287 return_value = bytearray_split_impl(self, sep, maxsplit);
288
289exit:
290 return return_value;
291}
292
293PyDoc_STRVAR(bytearray_partition__doc__,
294"partition($self, sep, /)\n"
295"--\n"
296"\n"
297"Partition the bytearray into three parts using the given separator.\n"
298"\n"
299"This will search for the separator sep in the bytearray. If the separator is\n"
300"found, returns a 3-tuple containing the part before the separator, the\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300301"separator itself, and the part after it as new bytearray objects.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300302"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300303"If the separator is not found, returns a 3-tuple containing the copy of the\n"
304"original bytearray object and two empty bytearray objects.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300305
306#define BYTEARRAY_PARTITION_METHODDEF \
307 {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__},
308
309PyDoc_STRVAR(bytearray_rpartition__doc__,
310"rpartition($self, sep, /)\n"
311"--\n"
312"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300313"Partition the bytearray into three parts using the given separator.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300314"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300315"This will search for the separator sep in the bytearray, starting at the end.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300316"If the separator is found, returns a 3-tuple containing the part before the\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300317"separator, the separator itself, and the part after it as new bytearray\n"
318"objects.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300319"\n"
320"If the separator is not found, returns a 3-tuple containing two empty bytearray\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300321"objects and the copy of the original bytearray object.");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300322
323#define BYTEARRAY_RPARTITION_METHODDEF \
324 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__},
325
326PyDoc_STRVAR(bytearray_rsplit__doc__,
327"rsplit($self, /, sep=None, maxsplit=-1)\n"
328"--\n"
329"\n"
330"Return a list of the sections in the bytearray, using sep as the delimiter.\n"
331"\n"
332" sep\n"
333" The delimiter according which to split the bytearray.\n"
334" None (the default value) means split on ASCII whitespace characters\n"
335" (space, tab, return, newline, formfeed, vertical tab).\n"
336" maxsplit\n"
337" Maximum number of splits to do.\n"
338" -1 (the default value) means no limit.\n"
339"\n"
340"Splitting is done starting at the end of the bytearray and working to the front.");
341
342#define BYTEARRAY_RSPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200343 {"rsplit", (PyCFunction)(void(*)(void))bytearray_rsplit, METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300344
345static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400346bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
347 Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300348
349static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200350bytearray_rsplit(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300351{
352 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300353 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200354 static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
355 PyObject *argsbuf[2];
356 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300357 PyObject *sep = Py_None;
358 Py_ssize_t maxsplit = -1;
359
Serhiy Storchaka31913912019-03-14 10:32:22 +0200360 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
361 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300362 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300363 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200364 if (!noptargs) {
365 goto skip_optional_pos;
366 }
367 if (args[0]) {
368 sep = args[0];
369 if (!--noptargs) {
370 goto skip_optional_pos;
371 }
372 }
373 if (PyFloat_Check(args[1])) {
374 PyErr_SetString(PyExc_TypeError,
375 "integer argument expected, got float" );
376 goto exit;
377 }
378 {
379 Py_ssize_t ival = -1;
380 PyObject *iobj = PyNumber_Index(args[1]);
381 if (iobj != NULL) {
382 ival = PyLong_AsSsize_t(iobj);
383 Py_DECREF(iobj);
384 }
385 if (ival == -1 && PyErr_Occurred()) {
386 goto exit;
387 }
388 maxsplit = ival;
389 }
390skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300391 return_value = bytearray_rsplit_impl(self, sep, maxsplit);
392
393exit:
394 return return_value;
395}
396
397PyDoc_STRVAR(bytearray_reverse__doc__,
398"reverse($self, /)\n"
399"--\n"
400"\n"
401"Reverse the order of the values in B in place.");
402
403#define BYTEARRAY_REVERSE_METHODDEF \
404 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__},
405
406static PyObject *
407bytearray_reverse_impl(PyByteArrayObject *self);
408
409static PyObject *
410bytearray_reverse(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
411{
412 return bytearray_reverse_impl(self);
413}
414
415PyDoc_STRVAR(bytearray_insert__doc__,
416"insert($self, index, item, /)\n"
417"--\n"
418"\n"
419"Insert a single item into the bytearray before the given index.\n"
420"\n"
421" index\n"
422" The index where the value is to be inserted.\n"
423" item\n"
424" The item to be inserted.");
425
426#define BYTEARRAY_INSERT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200427 {"insert", (PyCFunction)(void(*)(void))bytearray_insert, METH_FASTCALL, bytearray_insert__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300428
429static PyObject *
430bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item);
431
432static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200433bytearray_insert(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300434{
435 PyObject *return_value = NULL;
436 Py_ssize_t index;
437 int item;
438
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200439 if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
440 goto exit;
441 }
442 if (PyFloat_Check(args[0])) {
443 PyErr_SetString(PyExc_TypeError,
444 "integer argument expected, got float" );
445 goto exit;
446 }
447 {
448 Py_ssize_t ival = -1;
449 PyObject *iobj = PyNumber_Index(args[0]);
450 if (iobj != NULL) {
451 ival = PyLong_AsSsize_t(iobj);
452 Py_DECREF(iobj);
453 }
454 if (ival == -1 && PyErr_Occurred()) {
455 goto exit;
456 }
457 index = ival;
458 }
459 if (!_getbytevalue(args[1], &item)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100460 goto exit;
461 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300462 return_value = bytearray_insert_impl(self, index, item);
463
464exit:
465 return return_value;
466}
467
468PyDoc_STRVAR(bytearray_append__doc__,
469"append($self, item, /)\n"
470"--\n"
471"\n"
472"Append a single item to the end of the bytearray.\n"
473"\n"
474" item\n"
475" The item to be appended.");
476
477#define BYTEARRAY_APPEND_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300478 {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300479
480static PyObject *
481bytearray_append_impl(PyByteArrayObject *self, int item);
482
483static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300484bytearray_append(PyByteArrayObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300485{
486 PyObject *return_value = NULL;
487 int item;
488
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200489 if (!_getbytevalue(arg, &item)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300490 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300491 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300492 return_value = bytearray_append_impl(self, item);
493
494exit:
495 return return_value;
496}
497
498PyDoc_STRVAR(bytearray_extend__doc__,
499"extend($self, iterable_of_ints, /)\n"
500"--\n"
501"\n"
502"Append all the items from the iterator or sequence to the end of the bytearray.\n"
503"\n"
504" iterable_of_ints\n"
505" The iterable of items to append.");
506
507#define BYTEARRAY_EXTEND_METHODDEF \
508 {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__},
509
510PyDoc_STRVAR(bytearray_pop__doc__,
511"pop($self, index=-1, /)\n"
512"--\n"
513"\n"
514"Remove and return a single item from B.\n"
515"\n"
516" index\n"
517" The index from where to remove the item.\n"
518" -1 (the default value) means remove the last item.\n"
519"\n"
520"If no index argument is given, will pop the last item.");
521
522#define BYTEARRAY_POP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200523 {"pop", (PyCFunction)(void(*)(void))bytearray_pop, METH_FASTCALL, bytearray_pop__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300524
525static PyObject *
526bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index);
527
528static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200529bytearray_pop(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300530{
531 PyObject *return_value = NULL;
532 Py_ssize_t index = -1;
533
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200534 if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100535 goto exit;
536 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200537 if (nargs < 1) {
538 goto skip_optional;
539 }
540 if (PyFloat_Check(args[0])) {
541 PyErr_SetString(PyExc_TypeError,
542 "integer argument expected, got float" );
543 goto exit;
544 }
545 {
546 Py_ssize_t ival = -1;
547 PyObject *iobj = PyNumber_Index(args[0]);
548 if (iobj != NULL) {
549 ival = PyLong_AsSsize_t(iobj);
550 Py_DECREF(iobj);
551 }
552 if (ival == -1 && PyErr_Occurred()) {
553 goto exit;
554 }
555 index = ival;
556 }
557skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300558 return_value = bytearray_pop_impl(self, index);
559
560exit:
561 return return_value;
562}
563
564PyDoc_STRVAR(bytearray_remove__doc__,
565"remove($self, value, /)\n"
566"--\n"
567"\n"
568"Remove the first occurrence of a value in the bytearray.\n"
569"\n"
570" value\n"
571" The value to remove.");
572
573#define BYTEARRAY_REMOVE_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300574 {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300575
576static PyObject *
577bytearray_remove_impl(PyByteArrayObject *self, int value);
578
579static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300580bytearray_remove(PyByteArrayObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300581{
582 PyObject *return_value = NULL;
583 int value;
584
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200585 if (!_getbytevalue(arg, &value)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300586 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300587 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300588 return_value = bytearray_remove_impl(self, value);
589
590exit:
591 return return_value;
592}
593
594PyDoc_STRVAR(bytearray_strip__doc__,
595"strip($self, bytes=None, /)\n"
596"--\n"
597"\n"
598"Strip leading and trailing bytes contained in the argument.\n"
599"\n"
600"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
601
602#define BYTEARRAY_STRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200603 {"strip", (PyCFunction)(void(*)(void))bytearray_strip, METH_FASTCALL, bytearray_strip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300604
605static PyObject *
606bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes);
607
608static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200609bytearray_strip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300610{
611 PyObject *return_value = NULL;
612 PyObject *bytes = Py_None;
613
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200614 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100615 goto exit;
616 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200617 if (nargs < 1) {
618 goto skip_optional;
619 }
620 bytes = args[0];
621skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300622 return_value = bytearray_strip_impl(self, bytes);
623
624exit:
625 return return_value;
626}
627
628PyDoc_STRVAR(bytearray_lstrip__doc__,
629"lstrip($self, bytes=None, /)\n"
630"--\n"
631"\n"
632"Strip leading bytes contained in the argument.\n"
633"\n"
634"If the argument is omitted or None, strip leading ASCII whitespace.");
635
636#define BYTEARRAY_LSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200637 {"lstrip", (PyCFunction)(void(*)(void))bytearray_lstrip, METH_FASTCALL, bytearray_lstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300638
639static PyObject *
640bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes);
641
642static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200643bytearray_lstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300644{
645 PyObject *return_value = NULL;
646 PyObject *bytes = Py_None;
647
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200648 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100649 goto exit;
650 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200651 if (nargs < 1) {
652 goto skip_optional;
653 }
654 bytes = args[0];
655skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300656 return_value = bytearray_lstrip_impl(self, bytes);
657
658exit:
659 return return_value;
660}
661
662PyDoc_STRVAR(bytearray_rstrip__doc__,
663"rstrip($self, bytes=None, /)\n"
664"--\n"
665"\n"
666"Strip trailing bytes contained in the argument.\n"
667"\n"
668"If the argument is omitted or None, strip trailing ASCII whitespace.");
669
670#define BYTEARRAY_RSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200671 {"rstrip", (PyCFunction)(void(*)(void))bytearray_rstrip, METH_FASTCALL, bytearray_rstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300672
673static PyObject *
674bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes);
675
676static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200677bytearray_rstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300678{
679 PyObject *return_value = NULL;
680 PyObject *bytes = Py_None;
681
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200682 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100683 goto exit;
684 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200685 if (nargs < 1) {
686 goto skip_optional;
687 }
688 bytes = args[0];
689skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300690 return_value = bytearray_rstrip_impl(self, bytes);
691
692exit:
693 return return_value;
694}
695
696PyDoc_STRVAR(bytearray_decode__doc__,
697"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
698"--\n"
699"\n"
700"Decode the bytearray using the codec registered for encoding.\n"
701"\n"
702" encoding\n"
703" The encoding with which to decode the bytearray.\n"
704" errors\n"
705" The error handling scheme to use for the handling of decoding errors.\n"
706" The default is \'strict\' meaning that decoding errors raise a\n"
707" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
708" as well as any other name registered with codecs.register_error that\n"
709" can handle UnicodeDecodeErrors.");
710
711#define BYTEARRAY_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200712 {"decode", (PyCFunction)(void(*)(void))bytearray_decode, METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300713
714static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400715bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
716 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300717
718static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200719bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300720{
721 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300722 static const char * const _keywords[] = {"encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200723 static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0};
724 PyObject *argsbuf[2];
725 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300726 const char *encoding = NULL;
727 const char *errors = NULL;
728
Serhiy Storchaka31913912019-03-14 10:32:22 +0200729 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
730 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300731 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300732 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200733 if (!noptargs) {
734 goto skip_optional_pos;
735 }
736 if (args[0]) {
737 if (!PyUnicode_Check(args[0])) {
738 _PyArg_BadArgument("decode", 1, "str", args[0]);
739 goto exit;
740 }
741 Py_ssize_t encoding_length;
742 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
743 if (encoding == NULL) {
744 goto exit;
745 }
746 if (strlen(encoding) != (size_t)encoding_length) {
747 PyErr_SetString(PyExc_ValueError, "embedded null character");
748 goto exit;
749 }
750 if (!--noptargs) {
751 goto skip_optional_pos;
752 }
753 }
754 if (!PyUnicode_Check(args[1])) {
755 _PyArg_BadArgument("decode", 2, "str", args[1]);
756 goto exit;
757 }
758 Py_ssize_t errors_length;
759 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
760 if (errors == NULL) {
761 goto exit;
762 }
763 if (strlen(errors) != (size_t)errors_length) {
764 PyErr_SetString(PyExc_ValueError, "embedded null character");
765 goto exit;
766 }
767skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300768 return_value = bytearray_decode_impl(self, encoding, errors);
769
770exit:
771 return return_value;
772}
773
774PyDoc_STRVAR(bytearray_join__doc__,
775"join($self, iterable_of_bytes, /)\n"
776"--\n"
777"\n"
778"Concatenate any number of bytes/bytearray objects.\n"
779"\n"
780"The bytearray whose method is called is inserted in between each pair.\n"
781"\n"
782"The result is returned as a new bytearray object.");
783
784#define BYTEARRAY_JOIN_METHODDEF \
785 {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__},
786
787PyDoc_STRVAR(bytearray_splitlines__doc__,
788"splitlines($self, /, keepends=False)\n"
789"--\n"
790"\n"
791"Return a list of the lines in the bytearray, breaking at line boundaries.\n"
792"\n"
793"Line breaks are not included in the resulting list unless keepends is given and\n"
794"true.");
795
796#define BYTEARRAY_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200797 {"splitlines", (PyCFunction)(void(*)(void))bytearray_splitlines, METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300798
799static PyObject *
800bytearray_splitlines_impl(PyByteArrayObject *self, int keepends);
801
802static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200803bytearray_splitlines(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300804{
805 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300806 static const char * const _keywords[] = {"keepends", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200807 static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
808 PyObject *argsbuf[1];
809 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300810 int keepends = 0;
811
Serhiy Storchaka31913912019-03-14 10:32:22 +0200812 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
813 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300814 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300815 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200816 if (!noptargs) {
817 goto skip_optional_pos;
818 }
819 if (PyFloat_Check(args[0])) {
820 PyErr_SetString(PyExc_TypeError,
821 "integer argument expected, got float" );
822 goto exit;
823 }
824 keepends = _PyLong_AsInt(args[0]);
825 if (keepends == -1 && PyErr_Occurred()) {
826 goto exit;
827 }
828skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300829 return_value = bytearray_splitlines_impl(self, keepends);
830
831exit:
832 return return_value;
833}
834
835PyDoc_STRVAR(bytearray_fromhex__doc__,
836"fromhex($type, string, /)\n"
837"--\n"
838"\n"
839"Create a bytearray object from a string of hexadecimal numbers.\n"
840"\n"
841"Spaces between two numbers are accepted.\n"
842"Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')");
843
844#define BYTEARRAY_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300845 {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300846
847static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300848bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300849
850static PyObject *
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300851bytearray_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300852{
853 PyObject *return_value = NULL;
854 PyObject *string;
855
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200856 if (!PyUnicode_Check(arg)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200857 _PyArg_BadArgument("fromhex", 0, "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300858 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300859 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200860 if (PyUnicode_READY(arg) == -1) {
861 goto exit;
862 }
863 string = arg;
Serhiy Storchaka0855e702016-07-01 17:22:31 +0300864 return_value = bytearray_fromhex_impl(type, string);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300865
866exit:
867 return return_value;
868}
869
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700870PyDoc_STRVAR(bytearray_hex__doc__,
871"hex($self, /, sep=None, bytes_per_sep=1)\n"
872"--\n"
873"\n"
874"Create a str of hexadecimal numbers from a bytearray object.\n"
875"\n"
876" sep\n"
877" An optional single character or byte to separate hex bytes.\n"
878" bytes_per_sep\n"
879" How many bytes between separators. Positive values count from the\n"
880" right, negative values count from the left.\n"
881"\n"
882"Example:\n"
883">>> value = bytearray([0xb9, 0x01, 0xef])\n"
884">>> value.hex()\n"
885"\'b901ef\'\n"
886">>> value.hex(\':\')\n"
887"\'b9:01:ef\'\n"
888">>> value.hex(\':\', 2)\n"
889"\'b9:01ef\'\n"
890">>> value.hex(\':\', -2)\n"
891"\'b901:ef\'");
892
893#define BYTEARRAY_HEX_METHODDEF \
894 {"hex", (PyCFunction)(void(*)(void))bytearray_hex, METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__},
895
896static PyObject *
897bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep);
898
899static PyObject *
900bytearray_hex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
901{
902 PyObject *return_value = NULL;
903 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
904 static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0};
905 PyObject *argsbuf[2];
906 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
907 PyObject *sep = NULL;
908 int bytes_per_sep = 1;
909
910 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
911 if (!args) {
912 goto exit;
913 }
914 if (!noptargs) {
915 goto skip_optional_pos;
916 }
917 if (args[0]) {
918 sep = args[0];
919 if (!--noptargs) {
920 goto skip_optional_pos;
921 }
922 }
923 if (PyFloat_Check(args[1])) {
924 PyErr_SetString(PyExc_TypeError,
925 "integer argument expected, got float" );
926 goto exit;
927 }
928 bytes_per_sep = _PyLong_AsInt(args[1]);
929 if (bytes_per_sep == -1 && PyErr_Occurred()) {
930 goto exit;
931 }
932skip_optional_pos:
933 return_value = bytearray_hex_impl(self, sep, bytes_per_sep);
934
935exit:
936 return return_value;
937}
938
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300939PyDoc_STRVAR(bytearray_reduce__doc__,
940"__reduce__($self, /)\n"
941"--\n"
942"\n"
943"Return state information for pickling.");
944
945#define BYTEARRAY_REDUCE_METHODDEF \
946 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__},
947
948static PyObject *
949bytearray_reduce_impl(PyByteArrayObject *self);
950
951static PyObject *
952bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
953{
954 return bytearray_reduce_impl(self);
955}
956
957PyDoc_STRVAR(bytearray_reduce_ex__doc__,
958"__reduce_ex__($self, proto=0, /)\n"
959"--\n"
960"\n"
961"Return state information for pickling.");
962
963#define BYTEARRAY_REDUCE_EX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200964 {"__reduce_ex__", (PyCFunction)(void(*)(void))bytearray_reduce_ex, METH_FASTCALL, bytearray_reduce_ex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300965
966static PyObject *
967bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto);
968
969static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200970bytearray_reduce_ex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300971{
972 PyObject *return_value = NULL;
973 int proto = 0;
974
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200975 if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100976 goto exit;
977 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200978 if (nargs < 1) {
979 goto skip_optional;
980 }
981 if (PyFloat_Check(args[0])) {
982 PyErr_SetString(PyExc_TypeError,
983 "integer argument expected, got float" );
984 goto exit;
985 }
986 proto = _PyLong_AsInt(args[0]);
987 if (proto == -1 && PyErr_Occurred()) {
988 goto exit;
989 }
990skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300991 return_value = bytearray_reduce_ex_impl(self, proto);
992
993exit:
994 return return_value;
995}
996
997PyDoc_STRVAR(bytearray_sizeof__doc__,
998"__sizeof__($self, /)\n"
999"--\n"
1000"\n"
1001"Returns the size of the bytearray object in memory, in bytes.");
1002
1003#define BYTEARRAY_SIZEOF_METHODDEF \
1004 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__},
1005
1006static PyObject *
1007bytearray_sizeof_impl(PyByteArrayObject *self);
1008
1009static PyObject *
1010bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
1011{
1012 return bytearray_sizeof_impl(self);
1013}
Gregory P. Smith0c2f9302019-05-29 11:46:58 -07001014/*[clinic end generated code: output=7848247e5469ba1b input=a9049054013a1b77]*/