blob: 1345b644d176c3e1cec2856fd1f042d467457898 [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')) {
73 _PyArg_BadArgument("partition", "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')) {
116 _PyArg_BadArgument("rpartition", "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
Sylvain74453812017-06-10 06:51:48 +0200206 if (!_PyArg_UnpackStack(args, nargs, "strip",
207 0, 1,
208 &bytes)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100209 goto exit;
210 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300211 return_value = bytes_strip_impl(self, bytes);
212
213exit:
214 return return_value;
215}
216
217PyDoc_STRVAR(bytes_lstrip__doc__,
218"lstrip($self, bytes=None, /)\n"
219"--\n"
220"\n"
221"Strip leading bytes contained in the argument.\n"
222"\n"
223"If the argument is omitted or None, strip leading ASCII whitespace.");
224
225#define BYTES_LSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200226 {"lstrip", (PyCFunction)(void(*)(void))bytes_lstrip, METH_FASTCALL, bytes_lstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300227
228static PyObject *
229bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes);
230
231static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200232bytes_lstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300233{
234 PyObject *return_value = NULL;
235 PyObject *bytes = Py_None;
236
Sylvain74453812017-06-10 06:51:48 +0200237 if (!_PyArg_UnpackStack(args, nargs, "lstrip",
238 0, 1,
239 &bytes)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100240 goto exit;
241 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300242 return_value = bytes_lstrip_impl(self, bytes);
243
244exit:
245 return return_value;
246}
247
248PyDoc_STRVAR(bytes_rstrip__doc__,
249"rstrip($self, bytes=None, /)\n"
250"--\n"
251"\n"
252"Strip trailing bytes contained in the argument.\n"
253"\n"
254"If the argument is omitted or None, strip trailing ASCII whitespace.");
255
256#define BYTES_RSTRIP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200257 {"rstrip", (PyCFunction)(void(*)(void))bytes_rstrip, METH_FASTCALL, bytes_rstrip__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300258
259static PyObject *
260bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes);
261
262static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200263bytes_rstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300264{
265 PyObject *return_value = NULL;
266 PyObject *bytes = Py_None;
267
Sylvain74453812017-06-10 06:51:48 +0200268 if (!_PyArg_UnpackStack(args, nargs, "rstrip",
269 0, 1,
270 &bytes)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100271 goto exit;
272 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300273 return_value = bytes_rstrip_impl(self, bytes);
274
275exit:
276 return return_value;
277}
278
279PyDoc_STRVAR(bytes_translate__doc__,
Martin Panter1b6c6da2016-08-27 08:35:02 +0000280"translate($self, table, /, delete=b\'\')\n"
281"--\n"
282"\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300283"Return a copy with each character mapped by the given translation table.\n"
284"\n"
285" table\n"
286" Translation table, which must be a bytes object of length 256.\n"
287"\n"
Martin Panter1b6c6da2016-08-27 08:35:02 +0000288"All characters occurring in the optional argument delete are removed.\n"
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300289"The remaining characters are mapped through the given translation table.");
290
291#define BYTES_TRANSLATE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200292 {"translate", (PyCFunction)(void(*)(void))bytes_translate, METH_FASTCALL|METH_KEYWORDS, bytes_translate__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300293
294static PyObject *
Martin Panter1b6c6da2016-08-27 08:35:02 +0000295bytes_translate_impl(PyBytesObject *self, PyObject *table,
Larry Hastings89964c42015-04-14 18:07:59 -0400296 PyObject *deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300297
298static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200299bytes_translate(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300300{
301 PyObject *return_value = NULL;
Martin Panter1b6c6da2016-08-27 08:35:02 +0000302 static const char * const _keywords[] = {"", "delete", NULL};
303 static _PyArg_Parser _parser = {"O|O:translate", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300304 PyObject *table;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300305 PyObject *deletechars = NULL;
306
Victor Stinner3e1fad62017-01-17 01:29:01 +0100307 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Martin Panter1b6c6da2016-08-27 08:35:02 +0000308 &table, &deletechars)) {
309 goto exit;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300310 }
Martin Panter1b6c6da2016-08-27 08:35:02 +0000311 return_value = bytes_translate_impl(self, table, deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300312
313exit:
314 return return_value;
315}
316
317PyDoc_STRVAR(bytes_maketrans__doc__,
318"maketrans(frm, to, /)\n"
319"--\n"
320"\n"
321"Return a translation table useable for the bytes or bytearray translate method.\n"
322"\n"
323"The returned table will be one where each byte in frm is mapped to the byte at\n"
324"the same position in to.\n"
325"\n"
326"The bytes objects frm and to must be of the same length.");
327
328#define BYTES_MAKETRANS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200329 {"maketrans", (PyCFunction)(void(*)(void))bytes_maketrans, METH_FASTCALL|METH_STATIC, bytes_maketrans__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300330
331static PyObject *
332bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to);
333
334static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200335bytes_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300336{
337 PyObject *return_value = NULL;
338 Py_buffer frm = {NULL, NULL};
339 Py_buffer to = {NULL, NULL};
340
Sylvain74453812017-06-10 06:51:48 +0200341 if (!_PyArg_ParseStack(args, nargs, "y*y*:maketrans",
342 &frm, &to)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100343 goto exit;
344 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300345 return_value = bytes_maketrans_impl(&frm, &to);
346
347exit:
348 /* Cleanup for frm */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300349 if (frm.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300350 PyBuffer_Release(&frm);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300351 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300352 /* Cleanup for to */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300353 if (to.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300354 PyBuffer_Release(&to);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300355 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300356
357 return return_value;
358}
359
360PyDoc_STRVAR(bytes_replace__doc__,
361"replace($self, old, new, count=-1, /)\n"
362"--\n"
363"\n"
364"Return a copy with all occurrences of substring old replaced by new.\n"
365"\n"
366" count\n"
367" Maximum number of occurrences to replace.\n"
368" -1 (the default value) means replace all occurrences.\n"
369"\n"
370"If the optional argument count is given, only the first count occurrences are\n"
371"replaced.");
372
373#define BYTES_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200374 {"replace", (PyCFunction)(void(*)(void))bytes_replace, METH_FASTCALL, bytes_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300375
376static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300377bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
Larry Hastings89964c42015-04-14 18:07:59 -0400378 Py_ssize_t count);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300379
380static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200381bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300382{
383 PyObject *return_value = NULL;
384 Py_buffer old = {NULL, NULL};
385 Py_buffer new = {NULL, NULL};
386 Py_ssize_t count = -1;
387
Sylvain74453812017-06-10 06:51:48 +0200388 if (!_PyArg_ParseStack(args, nargs, "y*y*|n:replace",
389 &old, &new, &count)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100390 goto exit;
391 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300392 return_value = bytes_replace_impl(self, &old, &new, count);
393
394exit:
395 /* Cleanup for old */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300396 if (old.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300397 PyBuffer_Release(&old);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300398 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300399 /* Cleanup for new */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300400 if (new.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300401 PyBuffer_Release(&new);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300402 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300403
404 return return_value;
405}
406
407PyDoc_STRVAR(bytes_decode__doc__,
408"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
409"--\n"
410"\n"
411"Decode the bytes using the codec registered for encoding.\n"
412"\n"
413" encoding\n"
414" The encoding with which to decode the bytes.\n"
415" errors\n"
416" The error handling scheme to use for the handling of decoding errors.\n"
417" The default is \'strict\' meaning that decoding errors raise a\n"
418" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
419" as well as any other name registered with codecs.register_error that\n"
420" can handle UnicodeDecodeErrors.");
421
422#define BYTES_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200423 {"decode", (PyCFunction)(void(*)(void))bytes_decode, METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300424
425static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300426bytes_decode_impl(PyBytesObject *self, const char *encoding,
Larry Hastings89964c42015-04-14 18:07:59 -0400427 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300428
429static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200430bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300431{
432 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300433 static const char * const _keywords[] = {"encoding", "errors", NULL};
434 static _PyArg_Parser _parser = {"|ss:decode", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300435 const char *encoding = NULL;
436 const char *errors = NULL;
437
Victor Stinner3e1fad62017-01-17 01:29:01 +0100438 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300439 &encoding, &errors)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300440 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300441 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300442 return_value = bytes_decode_impl(self, encoding, errors);
443
444exit:
445 return return_value;
446}
447
448PyDoc_STRVAR(bytes_splitlines__doc__,
449"splitlines($self, /, keepends=False)\n"
450"--\n"
451"\n"
452"Return a list of the lines in the bytes, breaking at line boundaries.\n"
453"\n"
454"Line breaks are not included in the resulting list unless keepends is given and\n"
455"true.");
456
457#define BYTES_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200458 {"splitlines", (PyCFunction)(void(*)(void))bytes_splitlines, METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300459
460static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300461bytes_splitlines_impl(PyBytesObject *self, int keepends);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300462
463static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200464bytes_splitlines(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300465{
466 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300467 static const char * const _keywords[] = {"keepends", NULL};
468 static _PyArg_Parser _parser = {"|i:splitlines", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300469 int keepends = 0;
470
Victor Stinner3e1fad62017-01-17 01:29:01 +0100471 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300472 &keepends)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300473 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300474 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300475 return_value = bytes_splitlines_impl(self, keepends);
476
477exit:
478 return return_value;
479}
480
481PyDoc_STRVAR(bytes_fromhex__doc__,
482"fromhex($type, string, /)\n"
483"--\n"
484"\n"
485"Create a bytes object from a string of hexadecimal numbers.\n"
486"\n"
487"Spaces between two numbers are accepted.\n"
488"Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'.");
489
490#define BYTES_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300491 {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300492
493static PyObject *
494bytes_fromhex_impl(PyTypeObject *type, PyObject *string);
495
496static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300497bytes_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300498{
499 PyObject *return_value = NULL;
500 PyObject *string;
501
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200502 if (!PyUnicode_Check(arg)) {
503 _PyArg_BadArgument("fromhex", "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300504 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300505 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200506 if (PyUnicode_READY(arg) == -1) {
507 goto exit;
508 }
509 string = arg;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300510 return_value = bytes_fromhex_impl(type, string);
511
512exit:
513 return return_value;
514}
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200515/*[clinic end generated code: output=dc9aa04f0007ab11 input=a9049054013a1b77]*/