blob: 27ac6b106748ab7bc0d45ac55f92d198f104ded5 [file] [log] [blame]
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(bytes_split__doc__,
6"split($self, /, sep=None, maxsplit=-1)\n"
7"--\n"
8"\n"
9"Return a list of the sections in the bytes, using sep as the delimiter.\n"
10"\n"
11" sep\n"
12" The delimiter according which to split the bytes.\n"
13" None (the default value) means split on ASCII whitespace characters\n"
14" (space, tab, return, newline, formfeed, vertical tab).\n"
15" maxsplit\n"
16" Maximum number of splits to do.\n"
17" -1 (the default value) means no limit.");
18
19#define BYTES_SPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020020 {"split", (PyCFunction)(void(*)(void))bytes_split, METH_FASTCALL|METH_KEYWORDS, bytes_split__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030021
22static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +030023bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030024
25static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020026bytes_split(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030027{
28 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030029 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020030 static _PyArg_Parser _parser = {NULL, _keywords, "split", 0};
31 PyObject *argsbuf[2];
32 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030033 PyObject *sep = Py_None;
34 Py_ssize_t maxsplit = -1;
35
Serhiy Storchaka31913912019-03-14 10:32:22 +020036 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
37 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030038 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030039 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020040 if (!noptargs) {
41 goto skip_optional_pos;
42 }
43 if (args[0]) {
44 sep = args[0];
45 if (!--noptargs) {
46 goto skip_optional_pos;
47 }
48 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020049 {
50 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +030051 PyObject *iobj = _PyNumber_Index(args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +020052 if (iobj != NULL) {
53 ival = PyLong_AsSsize_t(iobj);
54 Py_DECREF(iobj);
55 }
56 if (ival == -1 && PyErr_Occurred()) {
57 goto exit;
58 }
59 maxsplit = ival;
60 }
61skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030062 return_value = bytes_split_impl(self, sep, maxsplit);
63
64exit:
65 return return_value;
66}
67
68PyDoc_STRVAR(bytes_partition__doc__,
69"partition($self, sep, /)\n"
70"--\n"
71"\n"
72"Partition the bytes into three parts using the given separator.\n"
73"\n"
74"This will search for the separator sep in the bytes. If the separator is found,\n"
75"returns a 3-tuple containing the part before the separator, the separator\n"
76"itself, and the part after it.\n"
77"\n"
78"If the separator is not found, returns a 3-tuple containing the original bytes\n"
79"object and two empty bytes objects.");
80
81#define BYTES_PARTITION_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030082 {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030083
84static PyObject *
85bytes_partition_impl(PyBytesObject *self, Py_buffer *sep);
86
87static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030088bytes_partition(PyBytesObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030089{
90 PyObject *return_value = NULL;
91 Py_buffer sep = {NULL, NULL};
92
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020093 if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
94 goto exit;
95 }
96 if (!PyBuffer_IsContiguous(&sep, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020097 _PyArg_BadArgument("partition", "argument", "contiguous buffer", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030098 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030099 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300100 return_value = bytes_partition_impl(self, &sep);
101
102exit:
103 /* Cleanup for sep */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300104 if (sep.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300105 PyBuffer_Release(&sep);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300106 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300107
108 return return_value;
109}
110
111PyDoc_STRVAR(bytes_rpartition__doc__,
112"rpartition($self, sep, /)\n"
113"--\n"
114"\n"
115"Partition the bytes into three parts using the given separator.\n"
116"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300117"This will search for the separator sep in the bytes, starting at the end. If\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300118"the separator is found, returns a 3-tuple containing the part before the\n"
119"separator, the separator itself, and the part after it.\n"
120"\n"
121"If the separator is not found, returns a 3-tuple containing two empty bytes\n"
122"objects and the original bytes object.");
123
124#define BYTES_RPARTITION_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300125 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300126
127static PyObject *
128bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep);
129
130static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300131bytes_rpartition(PyBytesObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300132{
133 PyObject *return_value = NULL;
134 Py_buffer sep = {NULL, NULL};
135
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200136 if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
137 goto exit;
138 }
139 if (!PyBuffer_IsContiguous(&sep, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200140 _PyArg_BadArgument("rpartition", "argument", "contiguous buffer", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300141 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300142 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300143 return_value = bytes_rpartition_impl(self, &sep);
144
145exit:
146 /* Cleanup for sep */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300147 if (sep.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300148 PyBuffer_Release(&sep);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300149 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300150
151 return return_value;
152}
153
154PyDoc_STRVAR(bytes_rsplit__doc__,
155"rsplit($self, /, sep=None, maxsplit=-1)\n"
156"--\n"
157"\n"
158"Return a list of the sections in the bytes, using sep as the delimiter.\n"
159"\n"
160" sep\n"
161" The delimiter according which to split the bytes.\n"
162" None (the default value) means split on ASCII whitespace characters\n"
163" (space, tab, return, newline, formfeed, vertical tab).\n"
164" maxsplit\n"
165" Maximum number of splits to do.\n"
166" -1 (the default value) means no limit.\n"
167"\n"
168"Splitting is done starting at the end of the bytes and working to the front.");
169
170#define BYTES_RSPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200171 {"rsplit", (PyCFunction)(void(*)(void))bytes_rsplit, METH_FASTCALL|METH_KEYWORDS, bytes_rsplit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300172
173static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300174bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300175
176static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200177bytes_rsplit(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300178{
179 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300180 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200181 static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
182 PyObject *argsbuf[2];
183 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300184 PyObject *sep = Py_None;
185 Py_ssize_t maxsplit = -1;
186
Serhiy Storchaka31913912019-03-14 10:32:22 +0200187 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
188 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300189 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300190 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200191 if (!noptargs) {
192 goto skip_optional_pos;
193 }
194 if (args[0]) {
195 sep = args[0];
196 if (!--noptargs) {
197 goto skip_optional_pos;
198 }
199 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200200 {
201 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300202 PyObject *iobj = _PyNumber_Index(args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200203 if (iobj != NULL) {
204 ival = PyLong_AsSsize_t(iobj);
205 Py_DECREF(iobj);
206 }
207 if (ival == -1 && PyErr_Occurred()) {
208 goto exit;
209 }
210 maxsplit = ival;
211 }
212skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300213 return_value = bytes_rsplit_impl(self, sep, maxsplit);
214
215exit:
216 return return_value;
217}
218
219PyDoc_STRVAR(bytes_join__doc__,
220"join($self, iterable_of_bytes, /)\n"
221"--\n"
222"\n"
223"Concatenate any number of bytes objects.\n"
224"\n"
225"The bytes whose method is called is inserted in between each pair.\n"
226"\n"
227"The result is returned as a new bytes object.\n"
228"\n"
229"Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'.");
230
231#define BYTES_JOIN_METHODDEF \
232 {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__},
233
234PyDoc_STRVAR(bytes_strip__doc__,
235"strip($self, bytes=None, /)\n"
236"--\n"
237"\n"
238"Strip leading and trailing bytes contained in the argument.\n"
239"\n"
240"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
241
242#define BYTES_STRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200243 {"strip", (PyCFunction)(void(*)(void))bytes_strip, METH_FASTCALL, bytes_strip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300244
245static PyObject *
246bytes_strip_impl(PyBytesObject *self, PyObject *bytes);
247
248static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200249bytes_strip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300250{
251 PyObject *return_value = NULL;
252 PyObject *bytes = Py_None;
253
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200254 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100255 goto exit;
256 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200257 if (nargs < 1) {
258 goto skip_optional;
259 }
260 bytes = args[0];
261skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300262 return_value = bytes_strip_impl(self, bytes);
263
264exit:
265 return return_value;
266}
267
268PyDoc_STRVAR(bytes_lstrip__doc__,
269"lstrip($self, bytes=None, /)\n"
270"--\n"
271"\n"
272"Strip leading bytes contained in the argument.\n"
273"\n"
274"If the argument is omitted or None, strip leading ASCII whitespace.");
275
276#define BYTES_LSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200277 {"lstrip", (PyCFunction)(void(*)(void))bytes_lstrip, METH_FASTCALL, bytes_lstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300278
279static PyObject *
280bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes);
281
282static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200283bytes_lstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300284{
285 PyObject *return_value = NULL;
286 PyObject *bytes = Py_None;
287
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200288 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100289 goto exit;
290 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200291 if (nargs < 1) {
292 goto skip_optional;
293 }
294 bytes = args[0];
295skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300296 return_value = bytes_lstrip_impl(self, bytes);
297
298exit:
299 return return_value;
300}
301
302PyDoc_STRVAR(bytes_rstrip__doc__,
303"rstrip($self, bytes=None, /)\n"
304"--\n"
305"\n"
306"Strip trailing bytes contained in the argument.\n"
307"\n"
308"If the argument is omitted or None, strip trailing ASCII whitespace.");
309
310#define BYTES_RSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200311 {"rstrip", (PyCFunction)(void(*)(void))bytes_rstrip, METH_FASTCALL, bytes_rstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300312
313static PyObject *
314bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes);
315
316static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200317bytes_rstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300318{
319 PyObject *return_value = NULL;
320 PyObject *bytes = Py_None;
321
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200322 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100323 goto exit;
324 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200325 if (nargs < 1) {
326 goto skip_optional;
327 }
328 bytes = args[0];
329skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300330 return_value = bytes_rstrip_impl(self, bytes);
331
332exit:
333 return return_value;
334}
335
336PyDoc_STRVAR(bytes_translate__doc__,
Martin Panter1b6c6da2016-08-27 08:35:02 +0000337"translate($self, table, /, delete=b\'\')\n"
338"--\n"
339"\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300340"Return a copy with each character mapped by the given translation table.\n"
341"\n"
342" table\n"
343" Translation table, which must be a bytes object of length 256.\n"
344"\n"
Martin Panter1b6c6da2016-08-27 08:35:02 +0000345"All characters occurring in the optional argument delete are removed.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300346"The remaining characters are mapped through the given translation table.");
347
348#define BYTES_TRANSLATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200349 {"translate", (PyCFunction)(void(*)(void))bytes_translate, METH_FASTCALL|METH_KEYWORDS, bytes_translate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300350
351static PyObject *
Martin Panter1b6c6da2016-08-27 08:35:02 +0000352bytes_translate_impl(PyBytesObject *self, PyObject *table,
Larry Hastings89964c42015-04-14 18:07:59 -0400353 PyObject *deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300354
355static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200356bytes_translate(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300357{
358 PyObject *return_value = NULL;
Martin Panter1b6c6da2016-08-27 08:35:02 +0000359 static const char * const _keywords[] = {"", "delete", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200360 static _PyArg_Parser _parser = {NULL, _keywords, "translate", 0};
361 PyObject *argsbuf[2];
362 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300363 PyObject *table;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300364 PyObject *deletechars = NULL;
365
Serhiy Storchaka31913912019-03-14 10:32:22 +0200366 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
367 if (!args) {
Martin Panter1b6c6da2016-08-27 08:35:02 +0000368 goto exit;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300369 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200370 table = args[0];
371 if (!noptargs) {
372 goto skip_optional_pos;
373 }
374 deletechars = args[1];
375skip_optional_pos:
Martin Panter1b6c6da2016-08-27 08:35:02 +0000376 return_value = bytes_translate_impl(self, table, deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300377
378exit:
379 return return_value;
380}
381
382PyDoc_STRVAR(bytes_maketrans__doc__,
383"maketrans(frm, to, /)\n"
384"--\n"
385"\n"
386"Return a translation table useable for the bytes or bytearray translate method.\n"
387"\n"
388"The returned table will be one where each byte in frm is mapped to the byte at\n"
389"the same position in to.\n"
390"\n"
391"The bytes objects frm and to must be of the same length.");
392
393#define BYTES_MAKETRANS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200394 {"maketrans", (PyCFunction)(void(*)(void))bytes_maketrans, METH_FASTCALL|METH_STATIC, bytes_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300395
396static PyObject *
397bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to);
398
399static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200400bytes_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300401{
402 PyObject *return_value = NULL;
403 Py_buffer frm = {NULL, NULL};
404 Py_buffer to = {NULL, NULL};
405
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200406 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
407 goto exit;
408 }
409 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
410 goto exit;
411 }
412 if (!PyBuffer_IsContiguous(&frm, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200413 _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200414 goto exit;
415 }
416 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
417 goto exit;
418 }
419 if (!PyBuffer_IsContiguous(&to, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200420 _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100421 goto exit;
422 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300423 return_value = bytes_maketrans_impl(&frm, &to);
424
425exit:
426 /* Cleanup for frm */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300427 if (frm.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300428 PyBuffer_Release(&frm);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300429 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300430 /* Cleanup for to */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300431 if (to.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300432 PyBuffer_Release(&to);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300433 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300434
435 return return_value;
436}
437
438PyDoc_STRVAR(bytes_replace__doc__,
439"replace($self, old, new, count=-1, /)\n"
440"--\n"
441"\n"
442"Return a copy with all occurrences of substring old replaced by new.\n"
443"\n"
444" count\n"
445" Maximum number of occurrences to replace.\n"
446" -1 (the default value) means replace all occurrences.\n"
447"\n"
448"If the optional argument count is given, only the first count occurrences are\n"
449"replaced.");
450
451#define BYTES_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200452 {"replace", (PyCFunction)(void(*)(void))bytes_replace, METH_FASTCALL, bytes_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300453
454static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300455bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
Larry Hastings89964c42015-04-14 18:07:59 -0400456 Py_ssize_t count);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300457
458static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200459bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300460{
461 PyObject *return_value = NULL;
462 Py_buffer old = {NULL, NULL};
463 Py_buffer new = {NULL, NULL};
464 Py_ssize_t count = -1;
465
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200466 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100467 goto exit;
468 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200469 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
470 goto exit;
471 }
472 if (!PyBuffer_IsContiguous(&old, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200473 _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200474 goto exit;
475 }
476 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
477 goto exit;
478 }
479 if (!PyBuffer_IsContiguous(&new, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200480 _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200481 goto exit;
482 }
483 if (nargs < 3) {
484 goto skip_optional;
485 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200486 {
487 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300488 PyObject *iobj = _PyNumber_Index(args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200489 if (iobj != NULL) {
490 ival = PyLong_AsSsize_t(iobj);
491 Py_DECREF(iobj);
492 }
493 if (ival == -1 && PyErr_Occurred()) {
494 goto exit;
495 }
496 count = ival;
497 }
498skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300499 return_value = bytes_replace_impl(self, &old, &new, count);
500
501exit:
502 /* Cleanup for old */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300503 if (old.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300504 PyBuffer_Release(&old);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300505 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300506 /* Cleanup for new */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300507 if (new.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300508 PyBuffer_Release(&new);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300509 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300510
511 return return_value;
512}
513
sweeneydea81849b2020-04-22 17:05:48 -0400514PyDoc_STRVAR(bytes_removeprefix__doc__,
515"removeprefix($self, prefix, /)\n"
516"--\n"
517"\n"
518"Return a bytes object with the given prefix string removed if present.\n"
519"\n"
520"If the bytes starts with the prefix string, return bytes[len(prefix):].\n"
521"Otherwise, return a copy of the original bytes.");
522
523#define BYTES_REMOVEPREFIX_METHODDEF \
524 {"removeprefix", (PyCFunction)bytes_removeprefix, METH_O, bytes_removeprefix__doc__},
525
526static PyObject *
527bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix);
528
529static PyObject *
530bytes_removeprefix(PyBytesObject *self, PyObject *arg)
531{
532 PyObject *return_value = NULL;
533 Py_buffer prefix = {NULL, NULL};
534
535 if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
536 goto exit;
537 }
538 if (!PyBuffer_IsContiguous(&prefix, 'C')) {
539 _PyArg_BadArgument("removeprefix", "argument", "contiguous buffer", arg);
540 goto exit;
541 }
542 return_value = bytes_removeprefix_impl(self, &prefix);
543
544exit:
545 /* Cleanup for prefix */
546 if (prefix.obj) {
547 PyBuffer_Release(&prefix);
548 }
549
550 return return_value;
551}
552
553PyDoc_STRVAR(bytes_removesuffix__doc__,
554"removesuffix($self, suffix, /)\n"
555"--\n"
556"\n"
557"Return a bytes object with the given suffix string removed if present.\n"
558"\n"
559"If the bytes ends with the suffix string and that suffix is not empty,\n"
560"return bytes[:-len(prefix)]. Otherwise, return a copy of the original\n"
561"bytes.");
562
563#define BYTES_REMOVESUFFIX_METHODDEF \
564 {"removesuffix", (PyCFunction)bytes_removesuffix, METH_O, bytes_removesuffix__doc__},
565
566static PyObject *
567bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix);
568
569static PyObject *
570bytes_removesuffix(PyBytesObject *self, PyObject *arg)
571{
572 PyObject *return_value = NULL;
573 Py_buffer suffix = {NULL, NULL};
574
575 if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
576 goto exit;
577 }
578 if (!PyBuffer_IsContiguous(&suffix, 'C')) {
579 _PyArg_BadArgument("removesuffix", "argument", "contiguous buffer", arg);
580 goto exit;
581 }
582 return_value = bytes_removesuffix_impl(self, &suffix);
583
584exit:
585 /* Cleanup for suffix */
586 if (suffix.obj) {
587 PyBuffer_Release(&suffix);
588 }
589
590 return return_value;
591}
592
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300593PyDoc_STRVAR(bytes_decode__doc__,
594"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
595"--\n"
596"\n"
597"Decode the bytes using the codec registered for encoding.\n"
598"\n"
599" encoding\n"
600" The encoding with which to decode the bytes.\n"
601" errors\n"
602" The error handling scheme to use for the handling of decoding errors.\n"
603" The default is \'strict\' meaning that decoding errors raise a\n"
604" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
605" as well as any other name registered with codecs.register_error that\n"
606" can handle UnicodeDecodeErrors.");
607
608#define BYTES_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200609 {"decode", (PyCFunction)(void(*)(void))bytes_decode, METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300610
611static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300612bytes_decode_impl(PyBytesObject *self, const char *encoding,
Larry Hastings89964c42015-04-14 18:07:59 -0400613 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300614
615static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200616bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300617{
618 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300619 static const char * const _keywords[] = {"encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200620 static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0};
621 PyObject *argsbuf[2];
622 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300623 const char *encoding = NULL;
624 const char *errors = NULL;
625
Serhiy Storchaka31913912019-03-14 10:32:22 +0200626 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
627 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300628 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300629 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200630 if (!noptargs) {
631 goto skip_optional_pos;
632 }
633 if (args[0]) {
634 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200635 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200636 goto exit;
637 }
638 Py_ssize_t encoding_length;
639 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
640 if (encoding == NULL) {
641 goto exit;
642 }
643 if (strlen(encoding) != (size_t)encoding_length) {
644 PyErr_SetString(PyExc_ValueError, "embedded null character");
645 goto exit;
646 }
647 if (!--noptargs) {
648 goto skip_optional_pos;
649 }
650 }
651 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200652 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200653 goto exit;
654 }
655 Py_ssize_t errors_length;
656 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
657 if (errors == NULL) {
658 goto exit;
659 }
660 if (strlen(errors) != (size_t)errors_length) {
661 PyErr_SetString(PyExc_ValueError, "embedded null character");
662 goto exit;
663 }
664skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300665 return_value = bytes_decode_impl(self, encoding, errors);
666
667exit:
668 return return_value;
669}
670
671PyDoc_STRVAR(bytes_splitlines__doc__,
672"splitlines($self, /, keepends=False)\n"
673"--\n"
674"\n"
675"Return a list of the lines in the bytes, breaking at line boundaries.\n"
676"\n"
677"Line breaks are not included in the resulting list unless keepends is given and\n"
678"true.");
679
680#define BYTES_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200681 {"splitlines", (PyCFunction)(void(*)(void))bytes_splitlines, METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300682
683static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300684bytes_splitlines_impl(PyBytesObject *self, int keepends);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300685
686static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200687bytes_splitlines(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300688{
689 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300690 static const char * const _keywords[] = {"keepends", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200691 static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
692 PyObject *argsbuf[1];
693 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300694 int keepends = 0;
695
Serhiy Storchaka31913912019-03-14 10:32:22 +0200696 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
697 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300698 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300699 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200700 if (!noptargs) {
701 goto skip_optional_pos;
702 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200703 keepends = _PyLong_AsInt(args[0]);
704 if (keepends == -1 && PyErr_Occurred()) {
705 goto exit;
706 }
707skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300708 return_value = bytes_splitlines_impl(self, keepends);
709
710exit:
711 return return_value;
712}
713
714PyDoc_STRVAR(bytes_fromhex__doc__,
715"fromhex($type, string, /)\n"
716"--\n"
717"\n"
718"Create a bytes object from a string of hexadecimal numbers.\n"
719"\n"
720"Spaces between two numbers are accepted.\n"
721"Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'.");
722
723#define BYTES_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300724 {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300725
726static PyObject *
727bytes_fromhex_impl(PyTypeObject *type, PyObject *string);
728
729static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300730bytes_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300731{
732 PyObject *return_value = NULL;
733 PyObject *string;
734
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200735 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200736 _PyArg_BadArgument("fromhex", "argument", "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300737 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300738 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200739 if (PyUnicode_READY(arg) == -1) {
740 goto exit;
741 }
742 string = arg;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300743 return_value = bytes_fromhex_impl(type, string);
744
745exit:
746 return return_value;
747}
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700748
749PyDoc_STRVAR(bytes_hex__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300750"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700751"--\n"
752"\n"
753"Create a str of hexadecimal numbers from a bytes object.\n"
754"\n"
755" sep\n"
756" An optional single character or byte to separate hex bytes.\n"
757" bytes_per_sep\n"
758" How many bytes between separators. Positive values count from the\n"
759" right, negative values count from the left.\n"
760"\n"
761"Example:\n"
762">>> value = b\'\\xb9\\x01\\xef\'\n"
763">>> value.hex()\n"
764"\'b901ef\'\n"
765">>> value.hex(\':\')\n"
766"\'b9:01:ef\'\n"
767">>> value.hex(\':\', 2)\n"
768"\'b9:01ef\'\n"
769">>> value.hex(\':\', -2)\n"
770"\'b901:ef\'");
771
772#define BYTES_HEX_METHODDEF \
773 {"hex", (PyCFunction)(void(*)(void))bytes_hex, METH_FASTCALL|METH_KEYWORDS, bytes_hex__doc__},
774
775static PyObject *
776bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep);
777
778static PyObject *
779bytes_hex(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
780{
781 PyObject *return_value = NULL;
782 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
783 static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0};
784 PyObject *argsbuf[2];
785 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
786 PyObject *sep = NULL;
787 int bytes_per_sep = 1;
788
789 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
790 if (!args) {
791 goto exit;
792 }
793 if (!noptargs) {
794 goto skip_optional_pos;
795 }
796 if (args[0]) {
797 sep = args[0];
798 if (!--noptargs) {
799 goto skip_optional_pos;
800 }
801 }
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700802 bytes_per_sep = _PyLong_AsInt(args[1]);
803 if (bytes_per_sep == -1 && PyErr_Occurred()) {
804 goto exit;
805 }
806skip_optional_pos:
807 return_value = bytes_hex_impl(self, sep, bytes_per_sep);
808
809exit:
810 return return_value;
811}
Serhiy Storchaka12f43342020-07-20 15:53:55 +0300812
813static PyObject *
814bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
815 const char *errors);
816
817static PyObject *
818bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
819{
820 PyObject *return_value = NULL;
821 static const char * const _keywords[] = {"source", "encoding", "errors", NULL};
822 static _PyArg_Parser _parser = {NULL, _keywords, "bytes", 0};
823 PyObject *argsbuf[3];
824 PyObject * const *fastargs;
825 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
826 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
827 PyObject *x = NULL;
828 const char *encoding = NULL;
829 const char *errors = NULL;
830
831 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
832 if (!fastargs) {
833 goto exit;
834 }
835 if (!noptargs) {
836 goto skip_optional_pos;
837 }
838 if (fastargs[0]) {
839 x = fastargs[0];
840 if (!--noptargs) {
841 goto skip_optional_pos;
842 }
843 }
844 if (fastargs[1]) {
845 if (!PyUnicode_Check(fastargs[1])) {
846 _PyArg_BadArgument("bytes", "argument 'encoding'", "str", fastargs[1]);
847 goto exit;
848 }
849 Py_ssize_t encoding_length;
850 encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
851 if (encoding == NULL) {
852 goto exit;
853 }
854 if (strlen(encoding) != (size_t)encoding_length) {
855 PyErr_SetString(PyExc_ValueError, "embedded null character");
856 goto exit;
857 }
858 if (!--noptargs) {
859 goto skip_optional_pos;
860 }
861 }
862 if (!PyUnicode_Check(fastargs[2])) {
863 _PyArg_BadArgument("bytes", "argument 'errors'", "str", fastargs[2]);
864 goto exit;
865 }
866 Py_ssize_t errors_length;
867 errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
868 if (errors == NULL) {
869 goto exit;
870 }
871 if (strlen(errors) != (size_t)errors_length) {
872 PyErr_SetString(PyExc_ValueError, "embedded null character");
873 goto exit;
874 }
875skip_optional_pos:
876 return_value = bytes_new_impl(type, x, encoding, errors);
877
878exit:
879 return return_value;
880}
881/*[clinic end generated code: output=6101b417d6a6a717 input=a9049054013a1b77]*/