blob: d75bbf1e5432536aebafb713d35f87906c304102 [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};
30 static _PyArg_Parser _parser = {"|On:split", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030031 PyObject *sep = Py_None;
32 Py_ssize_t maxsplit = -1;
33
Victor Stinner3e1fad62017-01-17 01:29:01 +010034 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030035 &sep, &maxsplit)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030036 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030037 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030038 return_value = bytes_split_impl(self, sep, maxsplit);
39
40exit:
41 return return_value;
42}
43
44PyDoc_STRVAR(bytes_partition__doc__,
45"partition($self, sep, /)\n"
46"--\n"
47"\n"
48"Partition the bytes into three parts using the given separator.\n"
49"\n"
50"This will search for the separator sep in the bytes. If the separator is found,\n"
51"returns a 3-tuple containing the part before the separator, the separator\n"
52"itself, and the part after it.\n"
53"\n"
54"If the separator is not found, returns a 3-tuple containing the original bytes\n"
55"object and two empty bytes objects.");
56
57#define BYTES_PARTITION_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030058 {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030059
60static PyObject *
61bytes_partition_impl(PyBytesObject *self, Py_buffer *sep);
62
63static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030064bytes_partition(PyBytesObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030065{
66 PyObject *return_value = NULL;
67 Py_buffer sep = {NULL, NULL};
68
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020069 if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
70 goto exit;
71 }
72 if (!PyBuffer_IsContiguous(&sep, 'C')) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020073 _PyArg_BadArgument("partition", 0, "contiguous buffer", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030074 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030075 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030076 return_value = bytes_partition_impl(self, &sep);
77
78exit:
79 /* Cleanup for sep */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030080 if (sep.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030081 PyBuffer_Release(&sep);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030082 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030083
84 return return_value;
85}
86
87PyDoc_STRVAR(bytes_rpartition__doc__,
88"rpartition($self, sep, /)\n"
89"--\n"
90"\n"
91"Partition the bytes into three parts using the given separator.\n"
92"\n"
Serhiy Storchakaa2314282017-10-29 02:11:54 +030093"This will search for the separator sep in the bytes, starting at the end. If\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030094"the separator is found, returns a 3-tuple containing the part before the\n"
95"separator, the separator itself, and the part after it.\n"
96"\n"
97"If the separator is not found, returns a 3-tuple containing two empty bytes\n"
98"objects and the original bytes object.");
99
100#define BYTES_RPARTITION_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300101 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300102
103static PyObject *
104bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep);
105
106static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300107bytes_rpartition(PyBytesObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300108{
109 PyObject *return_value = NULL;
110 Py_buffer sep = {NULL, NULL};
111
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200112 if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) {
113 goto exit;
114 }
115 if (!PyBuffer_IsContiguous(&sep, 'C')) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200116 _PyArg_BadArgument("rpartition", 0, "contiguous buffer", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300117 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300118 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300119 return_value = bytes_rpartition_impl(self, &sep);
120
121exit:
122 /* Cleanup for sep */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300123 if (sep.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300124 PyBuffer_Release(&sep);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300125 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300126
127 return return_value;
128}
129
130PyDoc_STRVAR(bytes_rsplit__doc__,
131"rsplit($self, /, sep=None, maxsplit=-1)\n"
132"--\n"
133"\n"
134"Return a list of the sections in the bytes, using sep as the delimiter.\n"
135"\n"
136" sep\n"
137" The delimiter according which to split the bytes.\n"
138" None (the default value) means split on ASCII whitespace characters\n"
139" (space, tab, return, newline, formfeed, vertical tab).\n"
140" maxsplit\n"
141" Maximum number of splits to do.\n"
142" -1 (the default value) means no limit.\n"
143"\n"
144"Splitting is done starting at the end of the bytes and working to the front.");
145
146#define BYTES_RSPLIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200147 {"rsplit", (PyCFunction)(void(*)(void))bytes_rsplit, METH_FASTCALL|METH_KEYWORDS, bytes_rsplit__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300148
149static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300150bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300151
152static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200153bytes_rsplit(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300154{
155 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300156 static const char * const _keywords[] = {"sep", "maxsplit", NULL};
157 static _PyArg_Parser _parser = {"|On:rsplit", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300158 PyObject *sep = Py_None;
159 Py_ssize_t maxsplit = -1;
160
Victor Stinner3e1fad62017-01-17 01:29:01 +0100161 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300162 &sep, &maxsplit)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300163 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300164 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300165 return_value = bytes_rsplit_impl(self, sep, maxsplit);
166
167exit:
168 return return_value;
169}
170
171PyDoc_STRVAR(bytes_join__doc__,
172"join($self, iterable_of_bytes, /)\n"
173"--\n"
174"\n"
175"Concatenate any number of bytes objects.\n"
176"\n"
177"The bytes whose method is called is inserted in between each pair.\n"
178"\n"
179"The result is returned as a new bytes object.\n"
180"\n"
181"Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'.");
182
183#define BYTES_JOIN_METHODDEF \
184 {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__},
185
186PyDoc_STRVAR(bytes_strip__doc__,
187"strip($self, bytes=None, /)\n"
188"--\n"
189"\n"
190"Strip leading and trailing bytes contained in the argument.\n"
191"\n"
192"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
193
194#define BYTES_STRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200195 {"strip", (PyCFunction)(void(*)(void))bytes_strip, METH_FASTCALL, bytes_strip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300196
197static PyObject *
198bytes_strip_impl(PyBytesObject *self, PyObject *bytes);
199
200static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200201bytes_strip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300202{
203 PyObject *return_value = NULL;
204 PyObject *bytes = Py_None;
205
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200206 if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100207 goto exit;
208 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200209 if (nargs < 1) {
210 goto skip_optional;
211 }
212 bytes = args[0];
213skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300214 return_value = bytes_strip_impl(self, bytes);
215
216exit:
217 return return_value;
218}
219
220PyDoc_STRVAR(bytes_lstrip__doc__,
221"lstrip($self, bytes=None, /)\n"
222"--\n"
223"\n"
224"Strip leading bytes contained in the argument.\n"
225"\n"
226"If the argument is omitted or None, strip leading ASCII whitespace.");
227
228#define BYTES_LSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200229 {"lstrip", (PyCFunction)(void(*)(void))bytes_lstrip, METH_FASTCALL, bytes_lstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300230
231static PyObject *
232bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes);
233
234static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200235bytes_lstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300236{
237 PyObject *return_value = NULL;
238 PyObject *bytes = Py_None;
239
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200240 if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100241 goto exit;
242 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200243 if (nargs < 1) {
244 goto skip_optional;
245 }
246 bytes = args[0];
247skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300248 return_value = bytes_lstrip_impl(self, bytes);
249
250exit:
251 return return_value;
252}
253
254PyDoc_STRVAR(bytes_rstrip__doc__,
255"rstrip($self, bytes=None, /)\n"
256"--\n"
257"\n"
258"Strip trailing bytes contained in the argument.\n"
259"\n"
260"If the argument is omitted or None, strip trailing ASCII whitespace.");
261
262#define BYTES_RSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200263 {"rstrip", (PyCFunction)(void(*)(void))bytes_rstrip, METH_FASTCALL, bytes_rstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300264
265static PyObject *
266bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes);
267
268static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200269bytes_rstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300270{
271 PyObject *return_value = NULL;
272 PyObject *bytes = Py_None;
273
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200274 if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100275 goto exit;
276 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200277 if (nargs < 1) {
278 goto skip_optional;
279 }
280 bytes = args[0];
281skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300282 return_value = bytes_rstrip_impl(self, bytes);
283
284exit:
285 return return_value;
286}
287
288PyDoc_STRVAR(bytes_translate__doc__,
Martin Panter1b6c6da2016-08-27 08:35:02 +0000289"translate($self, table, /, delete=b\'\')\n"
290"--\n"
291"\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300292"Return a copy with each character mapped by the given translation table.\n"
293"\n"
294" table\n"
295" Translation table, which must be a bytes object of length 256.\n"
296"\n"
Martin Panter1b6c6da2016-08-27 08:35:02 +0000297"All characters occurring in the optional argument delete are removed.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300298"The remaining characters are mapped through the given translation table.");
299
300#define BYTES_TRANSLATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200301 {"translate", (PyCFunction)(void(*)(void))bytes_translate, METH_FASTCALL|METH_KEYWORDS, bytes_translate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300302
303static PyObject *
Martin Panter1b6c6da2016-08-27 08:35:02 +0000304bytes_translate_impl(PyBytesObject *self, PyObject *table,
Larry Hastings89964c42015-04-14 18:07:59 -0400305 PyObject *deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300306
307static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200308bytes_translate(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300309{
310 PyObject *return_value = NULL;
Martin Panter1b6c6da2016-08-27 08:35:02 +0000311 static const char * const _keywords[] = {"", "delete", NULL};
312 static _PyArg_Parser _parser = {"O|O:translate", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300313 PyObject *table;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300314 PyObject *deletechars = NULL;
315
Victor Stinner3e1fad62017-01-17 01:29:01 +0100316 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Martin Panter1b6c6da2016-08-27 08:35:02 +0000317 &table, &deletechars)) {
318 goto exit;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300319 }
Martin Panter1b6c6da2016-08-27 08:35:02 +0000320 return_value = bytes_translate_impl(self, table, deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300321
322exit:
323 return return_value;
324}
325
326PyDoc_STRVAR(bytes_maketrans__doc__,
327"maketrans(frm, to, /)\n"
328"--\n"
329"\n"
330"Return a translation table useable for the bytes or bytearray translate method.\n"
331"\n"
332"The returned table will be one where each byte in frm is mapped to the byte at\n"
333"the same position in to.\n"
334"\n"
335"The bytes objects frm and to must be of the same length.");
336
337#define BYTES_MAKETRANS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200338 {"maketrans", (PyCFunction)(void(*)(void))bytes_maketrans, METH_FASTCALL|METH_STATIC, bytes_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300339
340static PyObject *
341bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to);
342
343static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200344bytes_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300345{
346 PyObject *return_value = NULL;
347 Py_buffer frm = {NULL, NULL};
348 Py_buffer to = {NULL, NULL};
349
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200350 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
351 goto exit;
352 }
353 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
354 goto exit;
355 }
356 if (!PyBuffer_IsContiguous(&frm, 'C')) {
357 _PyArg_BadArgument("maketrans", 1, "contiguous buffer", args[0]);
358 goto exit;
359 }
360 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
361 goto exit;
362 }
363 if (!PyBuffer_IsContiguous(&to, 'C')) {
364 _PyArg_BadArgument("maketrans", 2, "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100365 goto exit;
366 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300367 return_value = bytes_maketrans_impl(&frm, &to);
368
369exit:
370 /* Cleanup for frm */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300371 if (frm.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300372 PyBuffer_Release(&frm);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300373 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300374 /* Cleanup for to */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300375 if (to.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300376 PyBuffer_Release(&to);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300377 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300378
379 return return_value;
380}
381
382PyDoc_STRVAR(bytes_replace__doc__,
383"replace($self, old, new, count=-1, /)\n"
384"--\n"
385"\n"
386"Return a copy with all occurrences of substring old replaced by new.\n"
387"\n"
388" count\n"
389" Maximum number of occurrences to replace.\n"
390" -1 (the default value) means replace all occurrences.\n"
391"\n"
392"If the optional argument count is given, only the first count occurrences are\n"
393"replaced.");
394
395#define BYTES_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200396 {"replace", (PyCFunction)(void(*)(void))bytes_replace, METH_FASTCALL, bytes_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300397
398static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300399bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
Larry Hastings89964c42015-04-14 18:07:59 -0400400 Py_ssize_t count);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300401
402static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200403bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300404{
405 PyObject *return_value = NULL;
406 Py_buffer old = {NULL, NULL};
407 Py_buffer new = {NULL, NULL};
408 Py_ssize_t count = -1;
409
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200410 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100411 goto exit;
412 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200413 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
414 goto exit;
415 }
416 if (!PyBuffer_IsContiguous(&old, 'C')) {
417 _PyArg_BadArgument("replace", 1, "contiguous buffer", args[0]);
418 goto exit;
419 }
420 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
421 goto exit;
422 }
423 if (!PyBuffer_IsContiguous(&new, 'C')) {
424 _PyArg_BadArgument("replace", 2, "contiguous buffer", args[1]);
425 goto exit;
426 }
427 if (nargs < 3) {
428 goto skip_optional;
429 }
430 if (PyFloat_Check(args[2])) {
431 PyErr_SetString(PyExc_TypeError,
432 "integer argument expected, got float" );
433 goto exit;
434 }
435 {
436 Py_ssize_t ival = -1;
437 PyObject *iobj = PyNumber_Index(args[2]);
438 if (iobj != NULL) {
439 ival = PyLong_AsSsize_t(iobj);
440 Py_DECREF(iobj);
441 }
442 if (ival == -1 && PyErr_Occurred()) {
443 goto exit;
444 }
445 count = ival;
446 }
447skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300448 return_value = bytes_replace_impl(self, &old, &new, count);
449
450exit:
451 /* Cleanup for old */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300452 if (old.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300453 PyBuffer_Release(&old);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300454 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300455 /* Cleanup for new */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300456 if (new.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300457 PyBuffer_Release(&new);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300458 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300459
460 return return_value;
461}
462
463PyDoc_STRVAR(bytes_decode__doc__,
464"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
465"--\n"
466"\n"
467"Decode the bytes using the codec registered for encoding.\n"
468"\n"
469" encoding\n"
470" The encoding with which to decode the bytes.\n"
471" errors\n"
472" The error handling scheme to use for the handling of decoding errors.\n"
473" The default is \'strict\' meaning that decoding errors raise a\n"
474" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
475" as well as any other name registered with codecs.register_error that\n"
476" can handle UnicodeDecodeErrors.");
477
478#define BYTES_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200479 {"decode", (PyCFunction)(void(*)(void))bytes_decode, METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300480
481static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300482bytes_decode_impl(PyBytesObject *self, const char *encoding,
Larry Hastings89964c42015-04-14 18:07:59 -0400483 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300484
485static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200486bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300487{
488 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300489 static const char * const _keywords[] = {"encoding", "errors", NULL};
490 static _PyArg_Parser _parser = {"|ss:decode", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300491 const char *encoding = NULL;
492 const char *errors = NULL;
493
Victor Stinner3e1fad62017-01-17 01:29:01 +0100494 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300495 &encoding, &errors)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300496 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300497 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300498 return_value = bytes_decode_impl(self, encoding, errors);
499
500exit:
501 return return_value;
502}
503
504PyDoc_STRVAR(bytes_splitlines__doc__,
505"splitlines($self, /, keepends=False)\n"
506"--\n"
507"\n"
508"Return a list of the lines in the bytes, breaking at line boundaries.\n"
509"\n"
510"Line breaks are not included in the resulting list unless keepends is given and\n"
511"true.");
512
513#define BYTES_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200514 {"splitlines", (PyCFunction)(void(*)(void))bytes_splitlines, METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300515
516static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300517bytes_splitlines_impl(PyBytesObject *self, int keepends);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300518
519static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200520bytes_splitlines(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300521{
522 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300523 static const char * const _keywords[] = {"keepends", NULL};
524 static _PyArg_Parser _parser = {"|i:splitlines", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300525 int keepends = 0;
526
Victor Stinner3e1fad62017-01-17 01:29:01 +0100527 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300528 &keepends)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300529 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300530 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300531 return_value = bytes_splitlines_impl(self, keepends);
532
533exit:
534 return return_value;
535}
536
537PyDoc_STRVAR(bytes_fromhex__doc__,
538"fromhex($type, string, /)\n"
539"--\n"
540"\n"
541"Create a bytes object from a string of hexadecimal numbers.\n"
542"\n"
543"Spaces between two numbers are accepted.\n"
544"Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'.");
545
546#define BYTES_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300547 {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300548
549static PyObject *
550bytes_fromhex_impl(PyTypeObject *type, PyObject *string);
551
552static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300553bytes_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300554{
555 PyObject *return_value = NULL;
556 PyObject *string;
557
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200558 if (!PyUnicode_Check(arg)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200559 _PyArg_BadArgument("fromhex", 0, "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300560 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300561 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200562 if (PyUnicode_READY(arg) == -1) {
563 goto exit;
564 }
565 string = arg;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300566 return_value = bytes_fromhex_impl(type, string);
567
568exit:
569 return return_value;
570}
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200571/*[clinic end generated code: output=c6621bda84e63e51 input=a9049054013a1b77]*/