blob: 063a3777b49074e291cbee8d54085fd04f350c9a [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 }
49 if (PyFloat_Check(args[1])) {
50 PyErr_SetString(PyExc_TypeError,
51 "integer argument expected, got float" );
52 goto exit;
53 }
54 {
55 Py_ssize_t ival = -1;
56 PyObject *iobj = PyNumber_Index(args[1]);
57 if (iobj != NULL) {
58 ival = PyLong_AsSsize_t(iobj);
59 Py_DECREF(iobj);
60 }
61 if (ival == -1 && PyErr_Occurred()) {
62 goto exit;
63 }
64 maxsplit = ival;
65 }
66skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030067 return_value = bytes_split_impl(self, sep, maxsplit);
68
69exit:
70 return return_value;
71}
72
73PyDoc_STRVAR(bytes_partition__doc__,
74"partition($self, sep, /)\n"
75"--\n"
76"\n"
77"Partition the bytes into three parts using the given separator.\n"
78"\n"
79"This will search for the separator sep in the bytes. If the separator is found,\n"
80"returns a 3-tuple containing the part before the separator, the separator\n"
81"itself, and the part after it.\n"
82"\n"
83"If the separator is not found, returns a 3-tuple containing the original bytes\n"
84"object and two empty bytes objects.");
85
86#define BYTES_PARTITION_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030087 {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030088
89static PyObject *
90bytes_partition_impl(PyBytesObject *self, Py_buffer *sep);
91
92static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030093bytes_partition(PyBytesObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030094{
95 PyObject *return_value = NULL;
96 Py_buffer sep = {NULL, NULL};
97
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020098 if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
99 goto exit;
100 }
101 if (!PyBuffer_IsContiguous(&sep, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200102 _PyArg_BadArgument("partition", "argument", "contiguous buffer", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300103 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300104 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300105 return_value = bytes_partition_impl(self, &sep);
106
107exit:
108 /* Cleanup for sep */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300109 if (sep.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300110 PyBuffer_Release(&sep);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300111 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300112
113 return return_value;
114}
115
116PyDoc_STRVAR(bytes_rpartition__doc__,
117"rpartition($self, sep, /)\n"
118"--\n"
119"\n"
120"Partition the bytes into three parts using the given separator.\n"
121"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +0300122"This will search for the separator sep in the bytes, starting at the end. If\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300123"the separator is found, returns a 3-tuple containing the part before the\n"
124"separator, the separator itself, and the part after it.\n"
125"\n"
126"If the separator is not found, returns a 3-tuple containing two empty bytes\n"
127"objects and the original bytes object.");
128
129#define BYTES_RPARTITION_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300130 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300131
132static PyObject *
133bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep);
134
135static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300136bytes_rpartition(PyBytesObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300137{
138 PyObject *return_value = NULL;
139 Py_buffer sep = {NULL, NULL};
140
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200141 if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
142 goto exit;
143 }
144 if (!PyBuffer_IsContiguous(&sep, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200145 _PyArg_BadArgument("rpartition", "argument", "contiguous buffer", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300146 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300147 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300148 return_value = bytes_rpartition_impl(self, &sep);
149
150exit:
151 /* Cleanup for sep */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300152 if (sep.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300153 PyBuffer_Release(&sep);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300154 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300155
156 return return_value;
157}
158
159PyDoc_STRVAR(bytes_rsplit__doc__,
160"rsplit($self, /, sep=None, maxsplit=-1)\n"
161"--\n"
162"\n"
163"Return a list of the sections in the bytes, using sep as the delimiter.\n"
164"\n"
165" sep\n"
166" The delimiter according which to split the bytes.\n"
167" None (the default value) means split on ASCII whitespace characters\n"
168" (space, tab, return, newline, formfeed, vertical tab).\n"
169" maxsplit\n"
170" Maximum number of splits to do.\n"
171" -1 (the default value) means no limit.\n"
172"\n"
173"Splitting is done starting at the end of the bytes and working to the front.");
174
175#define BYTES_RSPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200176 {"rsplit", (PyCFunction)(void(*)(void))bytes_rsplit, METH_FASTCALL|METH_KEYWORDS, bytes_rsplit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300177
178static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300179bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300180
181static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200182bytes_rsplit(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300183{
184 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300185 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200186 static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0};
187 PyObject *argsbuf[2];
188 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300189 PyObject *sep = Py_None;
190 Py_ssize_t maxsplit = -1;
191
Serhiy Storchaka31913912019-03-14 10:32:22 +0200192 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
193 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300194 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300195 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200196 if (!noptargs) {
197 goto skip_optional_pos;
198 }
199 if (args[0]) {
200 sep = args[0];
201 if (!--noptargs) {
202 goto skip_optional_pos;
203 }
204 }
205 if (PyFloat_Check(args[1])) {
206 PyErr_SetString(PyExc_TypeError,
207 "integer argument expected, got float" );
208 goto exit;
209 }
210 {
211 Py_ssize_t ival = -1;
212 PyObject *iobj = PyNumber_Index(args[1]);
213 if (iobj != NULL) {
214 ival = PyLong_AsSsize_t(iobj);
215 Py_DECREF(iobj);
216 }
217 if (ival == -1 && PyErr_Occurred()) {
218 goto exit;
219 }
220 maxsplit = ival;
221 }
222skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300223 return_value = bytes_rsplit_impl(self, sep, maxsplit);
224
225exit:
226 return return_value;
227}
228
229PyDoc_STRVAR(bytes_join__doc__,
230"join($self, iterable_of_bytes, /)\n"
231"--\n"
232"\n"
233"Concatenate any number of bytes objects.\n"
234"\n"
235"The bytes whose method is called is inserted in between each pair.\n"
236"\n"
237"The result is returned as a new bytes object.\n"
238"\n"
239"Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'.");
240
241#define BYTES_JOIN_METHODDEF \
242 {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__},
243
244PyDoc_STRVAR(bytes_strip__doc__,
245"strip($self, bytes=None, /)\n"
246"--\n"
247"\n"
248"Strip leading and trailing bytes contained in the argument.\n"
249"\n"
250"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
251
252#define BYTES_STRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200253 {"strip", (PyCFunction)(void(*)(void))bytes_strip, METH_FASTCALL, bytes_strip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300254
255static PyObject *
256bytes_strip_impl(PyBytesObject *self, PyObject *bytes);
257
258static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200259bytes_strip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300260{
261 PyObject *return_value = NULL;
262 PyObject *bytes = Py_None;
263
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200264 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100265 goto exit;
266 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200267 if (nargs < 1) {
268 goto skip_optional;
269 }
270 bytes = args[0];
271skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300272 return_value = bytes_strip_impl(self, bytes);
273
274exit:
275 return return_value;
276}
277
278PyDoc_STRVAR(bytes_lstrip__doc__,
279"lstrip($self, bytes=None, /)\n"
280"--\n"
281"\n"
282"Strip leading bytes contained in the argument.\n"
283"\n"
284"If the argument is omitted or None, strip leading ASCII whitespace.");
285
286#define BYTES_LSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200287 {"lstrip", (PyCFunction)(void(*)(void))bytes_lstrip, METH_FASTCALL, bytes_lstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300288
289static PyObject *
290bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes);
291
292static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200293bytes_lstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300294{
295 PyObject *return_value = NULL;
296 PyObject *bytes = Py_None;
297
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200298 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100299 goto exit;
300 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200301 if (nargs < 1) {
302 goto skip_optional;
303 }
304 bytes = args[0];
305skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300306 return_value = bytes_lstrip_impl(self, bytes);
307
308exit:
309 return return_value;
310}
311
312PyDoc_STRVAR(bytes_rstrip__doc__,
313"rstrip($self, bytes=None, /)\n"
314"--\n"
315"\n"
316"Strip trailing bytes contained in the argument.\n"
317"\n"
318"If the argument is omitted or None, strip trailing ASCII whitespace.");
319
320#define BYTES_RSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200321 {"rstrip", (PyCFunction)(void(*)(void))bytes_rstrip, METH_FASTCALL, bytes_rstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300322
323static PyObject *
324bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes);
325
326static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200327bytes_rstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300328{
329 PyObject *return_value = NULL;
330 PyObject *bytes = Py_None;
331
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200332 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100333 goto exit;
334 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200335 if (nargs < 1) {
336 goto skip_optional;
337 }
338 bytes = args[0];
339skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300340 return_value = bytes_rstrip_impl(self, bytes);
341
342exit:
343 return return_value;
344}
345
346PyDoc_STRVAR(bytes_translate__doc__,
Martin Panter1b6c6da2016-08-27 08:35:02 +0000347"translate($self, table, /, delete=b\'\')\n"
348"--\n"
349"\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300350"Return a copy with each character mapped by the given translation table.\n"
351"\n"
352" table\n"
353" Translation table, which must be a bytes object of length 256.\n"
354"\n"
Martin Panter1b6c6da2016-08-27 08:35:02 +0000355"All characters occurring in the optional argument delete are removed.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300356"The remaining characters are mapped through the given translation table.");
357
358#define BYTES_TRANSLATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200359 {"translate", (PyCFunction)(void(*)(void))bytes_translate, METH_FASTCALL|METH_KEYWORDS, bytes_translate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300360
361static PyObject *
Martin Panter1b6c6da2016-08-27 08:35:02 +0000362bytes_translate_impl(PyBytesObject *self, PyObject *table,
Larry Hastings89964c42015-04-14 18:07:59 -0400363 PyObject *deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300364
365static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200366bytes_translate(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300367{
368 PyObject *return_value = NULL;
Martin Panter1b6c6da2016-08-27 08:35:02 +0000369 static const char * const _keywords[] = {"", "delete", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200370 static _PyArg_Parser _parser = {NULL, _keywords, "translate", 0};
371 PyObject *argsbuf[2];
372 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300373 PyObject *table;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300374 PyObject *deletechars = NULL;
375
Serhiy Storchaka31913912019-03-14 10:32:22 +0200376 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
377 if (!args) {
Martin Panter1b6c6da2016-08-27 08:35:02 +0000378 goto exit;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300379 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200380 table = args[0];
381 if (!noptargs) {
382 goto skip_optional_pos;
383 }
384 deletechars = args[1];
385skip_optional_pos:
Martin Panter1b6c6da2016-08-27 08:35:02 +0000386 return_value = bytes_translate_impl(self, table, deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300387
388exit:
389 return return_value;
390}
391
392PyDoc_STRVAR(bytes_maketrans__doc__,
393"maketrans(frm, to, /)\n"
394"--\n"
395"\n"
396"Return a translation table useable for the bytes or bytearray translate method.\n"
397"\n"
398"The returned table will be one where each byte in frm is mapped to the byte at\n"
399"the same position in to.\n"
400"\n"
401"The bytes objects frm and to must be of the same length.");
402
403#define BYTES_MAKETRANS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200404 {"maketrans", (PyCFunction)(void(*)(void))bytes_maketrans, METH_FASTCALL|METH_STATIC, bytes_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300405
406static PyObject *
407bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to);
408
409static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200410bytes_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300411{
412 PyObject *return_value = NULL;
413 Py_buffer frm = {NULL, NULL};
414 Py_buffer to = {NULL, NULL};
415
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200416 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
417 goto exit;
418 }
419 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
420 goto exit;
421 }
422 if (!PyBuffer_IsContiguous(&frm, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200423 _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200424 goto exit;
425 }
426 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
427 goto exit;
428 }
429 if (!PyBuffer_IsContiguous(&to, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200430 _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100431 goto exit;
432 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300433 return_value = bytes_maketrans_impl(&frm, &to);
434
435exit:
436 /* Cleanup for frm */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300437 if (frm.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300438 PyBuffer_Release(&frm);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300439 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300440 /* Cleanup for to */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300441 if (to.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300442 PyBuffer_Release(&to);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300443 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300444
445 return return_value;
446}
447
448PyDoc_STRVAR(bytes_replace__doc__,
449"replace($self, old, new, count=-1, /)\n"
450"--\n"
451"\n"
452"Return a copy with all occurrences of substring old replaced by new.\n"
453"\n"
454" count\n"
455" Maximum number of occurrences to replace.\n"
456" -1 (the default value) means replace all occurrences.\n"
457"\n"
458"If the optional argument count is given, only the first count occurrences are\n"
459"replaced.");
460
461#define BYTES_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200462 {"replace", (PyCFunction)(void(*)(void))bytes_replace, METH_FASTCALL, bytes_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300463
464static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300465bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
Larry Hastings89964c42015-04-14 18:07:59 -0400466 Py_ssize_t count);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300467
468static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200469bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300470{
471 PyObject *return_value = NULL;
472 Py_buffer old = {NULL, NULL};
473 Py_buffer new = {NULL, NULL};
474 Py_ssize_t count = -1;
475
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200476 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100477 goto exit;
478 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200479 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
480 goto exit;
481 }
482 if (!PyBuffer_IsContiguous(&old, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200483 _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200484 goto exit;
485 }
486 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
487 goto exit;
488 }
489 if (!PyBuffer_IsContiguous(&new, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200490 _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200491 goto exit;
492 }
493 if (nargs < 3) {
494 goto skip_optional;
495 }
496 if (PyFloat_Check(args[2])) {
497 PyErr_SetString(PyExc_TypeError,
498 "integer argument expected, got float" );
499 goto exit;
500 }
501 {
502 Py_ssize_t ival = -1;
503 PyObject *iobj = PyNumber_Index(args[2]);
504 if (iobj != NULL) {
505 ival = PyLong_AsSsize_t(iobj);
506 Py_DECREF(iobj);
507 }
508 if (ival == -1 && PyErr_Occurred()) {
509 goto exit;
510 }
511 count = ival;
512 }
513skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300514 return_value = bytes_replace_impl(self, &old, &new, count);
515
516exit:
517 /* Cleanup for old */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300518 if (old.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300519 PyBuffer_Release(&old);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300520 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300521 /* Cleanup for new */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300522 if (new.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300523 PyBuffer_Release(&new);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300524 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300525
526 return return_value;
527}
528
sweeneydea81849b2020-04-22 17:05:48 -0400529PyDoc_STRVAR(bytes_removeprefix__doc__,
530"removeprefix($self, prefix, /)\n"
531"--\n"
532"\n"
533"Return a bytes object with the given prefix string removed if present.\n"
534"\n"
535"If the bytes starts with the prefix string, return bytes[len(prefix):].\n"
536"Otherwise, return a copy of the original bytes.");
537
538#define BYTES_REMOVEPREFIX_METHODDEF \
539 {"removeprefix", (PyCFunction)bytes_removeprefix, METH_O, bytes_removeprefix__doc__},
540
541static PyObject *
542bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix);
543
544static PyObject *
545bytes_removeprefix(PyBytesObject *self, PyObject *arg)
546{
547 PyObject *return_value = NULL;
548 Py_buffer prefix = {NULL, NULL};
549
550 if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) {
551 goto exit;
552 }
553 if (!PyBuffer_IsContiguous(&prefix, 'C')) {
554 _PyArg_BadArgument("removeprefix", "argument", "contiguous buffer", arg);
555 goto exit;
556 }
557 return_value = bytes_removeprefix_impl(self, &prefix);
558
559exit:
560 /* Cleanup for prefix */
561 if (prefix.obj) {
562 PyBuffer_Release(&prefix);
563 }
564
565 return return_value;
566}
567
568PyDoc_STRVAR(bytes_removesuffix__doc__,
569"removesuffix($self, suffix, /)\n"
570"--\n"
571"\n"
572"Return a bytes object with the given suffix string removed if present.\n"
573"\n"
574"If the bytes ends with the suffix string and that suffix is not empty,\n"
575"return bytes[:-len(prefix)]. Otherwise, return a copy of the original\n"
576"bytes.");
577
578#define BYTES_REMOVESUFFIX_METHODDEF \
579 {"removesuffix", (PyCFunction)bytes_removesuffix, METH_O, bytes_removesuffix__doc__},
580
581static PyObject *
582bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix);
583
584static PyObject *
585bytes_removesuffix(PyBytesObject *self, PyObject *arg)
586{
587 PyObject *return_value = NULL;
588 Py_buffer suffix = {NULL, NULL};
589
590 if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) {
591 goto exit;
592 }
593 if (!PyBuffer_IsContiguous(&suffix, 'C')) {
594 _PyArg_BadArgument("removesuffix", "argument", "contiguous buffer", arg);
595 goto exit;
596 }
597 return_value = bytes_removesuffix_impl(self, &suffix);
598
599exit:
600 /* Cleanup for suffix */
601 if (suffix.obj) {
602 PyBuffer_Release(&suffix);
603 }
604
605 return return_value;
606}
607
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300608PyDoc_STRVAR(bytes_decode__doc__,
609"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
610"--\n"
611"\n"
612"Decode the bytes using the codec registered for encoding.\n"
613"\n"
614" encoding\n"
615" The encoding with which to decode the bytes.\n"
616" errors\n"
617" The error handling scheme to use for the handling of decoding errors.\n"
618" The default is \'strict\' meaning that decoding errors raise a\n"
619" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
620" as well as any other name registered with codecs.register_error that\n"
621" can handle UnicodeDecodeErrors.");
622
623#define BYTES_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200624 {"decode", (PyCFunction)(void(*)(void))bytes_decode, METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300625
626static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300627bytes_decode_impl(PyBytesObject *self, const char *encoding,
Larry Hastings89964c42015-04-14 18:07:59 -0400628 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300629
630static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200631bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300632{
633 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300634 static const char * const _keywords[] = {"encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200635 static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0};
636 PyObject *argsbuf[2];
637 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300638 const char *encoding = NULL;
639 const char *errors = NULL;
640
Serhiy Storchaka31913912019-03-14 10:32:22 +0200641 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
642 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300643 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300644 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200645 if (!noptargs) {
646 goto skip_optional_pos;
647 }
648 if (args[0]) {
649 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200650 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200651 goto exit;
652 }
653 Py_ssize_t encoding_length;
654 encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
655 if (encoding == NULL) {
656 goto exit;
657 }
658 if (strlen(encoding) != (size_t)encoding_length) {
659 PyErr_SetString(PyExc_ValueError, "embedded null character");
660 goto exit;
661 }
662 if (!--noptargs) {
663 goto skip_optional_pos;
664 }
665 }
666 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200667 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200668 goto exit;
669 }
670 Py_ssize_t errors_length;
671 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
672 if (errors == NULL) {
673 goto exit;
674 }
675 if (strlen(errors) != (size_t)errors_length) {
676 PyErr_SetString(PyExc_ValueError, "embedded null character");
677 goto exit;
678 }
679skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300680 return_value = bytes_decode_impl(self, encoding, errors);
681
682exit:
683 return return_value;
684}
685
686PyDoc_STRVAR(bytes_splitlines__doc__,
687"splitlines($self, /, keepends=False)\n"
688"--\n"
689"\n"
690"Return a list of the lines in the bytes, breaking at line boundaries.\n"
691"\n"
692"Line breaks are not included in the resulting list unless keepends is given and\n"
693"true.");
694
695#define BYTES_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200696 {"splitlines", (PyCFunction)(void(*)(void))bytes_splitlines, METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300697
698static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300699bytes_splitlines_impl(PyBytesObject *self, int keepends);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300700
701static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200702bytes_splitlines(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300703{
704 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300705 static const char * const _keywords[] = {"keepends", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200706 static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0};
707 PyObject *argsbuf[1];
708 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300709 int keepends = 0;
710
Serhiy Storchaka31913912019-03-14 10:32:22 +0200711 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
712 if (!args) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300713 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300714 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200715 if (!noptargs) {
716 goto skip_optional_pos;
717 }
718 if (PyFloat_Check(args[0])) {
719 PyErr_SetString(PyExc_TypeError,
720 "integer argument expected, got float" );
721 goto exit;
722 }
723 keepends = _PyLong_AsInt(args[0]);
724 if (keepends == -1 && PyErr_Occurred()) {
725 goto exit;
726 }
727skip_optional_pos:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300728 return_value = bytes_splitlines_impl(self, keepends);
729
730exit:
731 return return_value;
732}
733
734PyDoc_STRVAR(bytes_fromhex__doc__,
735"fromhex($type, string, /)\n"
736"--\n"
737"\n"
738"Create a bytes object from a string of hexadecimal numbers.\n"
739"\n"
740"Spaces between two numbers are accepted.\n"
741"Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'.");
742
743#define BYTES_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300744 {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300745
746static PyObject *
747bytes_fromhex_impl(PyTypeObject *type, PyObject *string);
748
749static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300750bytes_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300751{
752 PyObject *return_value = NULL;
753 PyObject *string;
754
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200755 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200756 _PyArg_BadArgument("fromhex", "argument", "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300757 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300758 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200759 if (PyUnicode_READY(arg) == -1) {
760 goto exit;
761 }
762 string = arg;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300763 return_value = bytes_fromhex_impl(type, string);
764
765exit:
766 return return_value;
767}
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700768
769PyDoc_STRVAR(bytes_hex__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300770"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700771"--\n"
772"\n"
773"Create a str of hexadecimal numbers from a bytes object.\n"
774"\n"
775" sep\n"
776" An optional single character or byte to separate hex bytes.\n"
777" bytes_per_sep\n"
778" How many bytes between separators. Positive values count from the\n"
779" right, negative values count from the left.\n"
780"\n"
781"Example:\n"
782">>> value = b\'\\xb9\\x01\\xef\'\n"
783">>> value.hex()\n"
784"\'b901ef\'\n"
785">>> value.hex(\':\')\n"
786"\'b9:01:ef\'\n"
787">>> value.hex(\':\', 2)\n"
788"\'b9:01ef\'\n"
789">>> value.hex(\':\', -2)\n"
790"\'b901:ef\'");
791
792#define BYTES_HEX_METHODDEF \
793 {"hex", (PyCFunction)(void(*)(void))bytes_hex, METH_FASTCALL|METH_KEYWORDS, bytes_hex__doc__},
794
795static PyObject *
796bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep);
797
798static PyObject *
799bytes_hex(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
800{
801 PyObject *return_value = NULL;
802 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
803 static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0};
804 PyObject *argsbuf[2];
805 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
806 PyObject *sep = NULL;
807 int bytes_per_sep = 1;
808
809 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
810 if (!args) {
811 goto exit;
812 }
813 if (!noptargs) {
814 goto skip_optional_pos;
815 }
816 if (args[0]) {
817 sep = args[0];
818 if (!--noptargs) {
819 goto skip_optional_pos;
820 }
821 }
822 if (PyFloat_Check(args[1])) {
823 PyErr_SetString(PyExc_TypeError,
824 "integer argument expected, got float" );
825 goto exit;
826 }
827 bytes_per_sep = _PyLong_AsInt(args[1]);
828 if (bytes_per_sep == -1 && PyErr_Occurred()) {
829 goto exit;
830 }
831skip_optional_pos:
832 return_value = bytes_hex_impl(self, sep, bytes_per_sep);
833
834exit:
835 return return_value;
836}
sweeneydea81849b2020-04-22 17:05:48 -0400837/*[clinic end generated code: output=220388917d7bf751 input=a9049054013a1b77]*/