blob: 4e754d91af39ac316f5f3f9a7b72862c48622b55 [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
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
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200341 if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) {
342 goto exit;
343 }
344 if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) {
345 goto exit;
346 }
347 if (!PyBuffer_IsContiguous(&frm, 'C')) {
348 _PyArg_BadArgument("maketrans", 1, "contiguous buffer", args[0]);
349 goto exit;
350 }
351 if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) {
352 goto exit;
353 }
354 if (!PyBuffer_IsContiguous(&to, 'C')) {
355 _PyArg_BadArgument("maketrans", 2, "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100356 goto exit;
357 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300358 return_value = bytes_maketrans_impl(&frm, &to);
359
360exit:
361 /* Cleanup for frm */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300362 if (frm.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300363 PyBuffer_Release(&frm);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300364 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300365 /* Cleanup for to */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300366 if (to.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300367 PyBuffer_Release(&to);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300368 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300369
370 return return_value;
371}
372
373PyDoc_STRVAR(bytes_replace__doc__,
374"replace($self, old, new, count=-1, /)\n"
375"--\n"
376"\n"
377"Return a copy with all occurrences of substring old replaced by new.\n"
378"\n"
379" count\n"
380" Maximum number of occurrences to replace.\n"
381" -1 (the default value) means replace all occurrences.\n"
382"\n"
383"If the optional argument count is given, only the first count occurrences are\n"
384"replaced.");
385
386#define BYTES_REPLACE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200387 {"replace", (PyCFunction)(void(*)(void))bytes_replace, METH_FASTCALL, bytes_replace__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300388
389static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300390bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
Larry Hastings89964c42015-04-14 18:07:59 -0400391 Py_ssize_t count);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300392
393static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200394bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300395{
396 PyObject *return_value = NULL;
397 Py_buffer old = {NULL, NULL};
398 Py_buffer new = {NULL, NULL};
399 Py_ssize_t count = -1;
400
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200401 if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100402 goto exit;
403 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200404 if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
405 goto exit;
406 }
407 if (!PyBuffer_IsContiguous(&old, 'C')) {
408 _PyArg_BadArgument("replace", 1, "contiguous buffer", args[0]);
409 goto exit;
410 }
411 if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
412 goto exit;
413 }
414 if (!PyBuffer_IsContiguous(&new, 'C')) {
415 _PyArg_BadArgument("replace", 2, "contiguous buffer", args[1]);
416 goto exit;
417 }
418 if (nargs < 3) {
419 goto skip_optional;
420 }
421 if (PyFloat_Check(args[2])) {
422 PyErr_SetString(PyExc_TypeError,
423 "integer argument expected, got float" );
424 goto exit;
425 }
426 {
427 Py_ssize_t ival = -1;
428 PyObject *iobj = PyNumber_Index(args[2]);
429 if (iobj != NULL) {
430 ival = PyLong_AsSsize_t(iobj);
431 Py_DECREF(iobj);
432 }
433 if (ival == -1 && PyErr_Occurred()) {
434 goto exit;
435 }
436 count = ival;
437 }
438skip_optional:
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300439 return_value = bytes_replace_impl(self, &old, &new, count);
440
441exit:
442 /* Cleanup for old */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300443 if (old.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300444 PyBuffer_Release(&old);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300445 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300446 /* Cleanup for new */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300447 if (new.obj) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300448 PyBuffer_Release(&new);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300449 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300450
451 return return_value;
452}
453
454PyDoc_STRVAR(bytes_decode__doc__,
455"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
456"--\n"
457"\n"
458"Decode the bytes using the codec registered for encoding.\n"
459"\n"
460" encoding\n"
461" The encoding with which to decode the bytes.\n"
462" errors\n"
463" The error handling scheme to use for the handling of decoding errors.\n"
464" The default is \'strict\' meaning that decoding errors raise a\n"
465" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
466" as well as any other name registered with codecs.register_error that\n"
467" can handle UnicodeDecodeErrors.");
468
469#define BYTES_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200470 {"decode", (PyCFunction)(void(*)(void))bytes_decode, METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300471
472static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300473bytes_decode_impl(PyBytesObject *self, const char *encoding,
Larry Hastings89964c42015-04-14 18:07:59 -0400474 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300475
476static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200477bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300478{
479 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300480 static const char * const _keywords[] = {"encoding", "errors", NULL};
481 static _PyArg_Parser _parser = {"|ss:decode", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300482 const char *encoding = NULL;
483 const char *errors = NULL;
484
Victor Stinner3e1fad62017-01-17 01:29:01 +0100485 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300486 &encoding, &errors)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300487 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300488 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300489 return_value = bytes_decode_impl(self, encoding, errors);
490
491exit:
492 return return_value;
493}
494
495PyDoc_STRVAR(bytes_splitlines__doc__,
496"splitlines($self, /, keepends=False)\n"
497"--\n"
498"\n"
499"Return a list of the lines in the bytes, breaking at line boundaries.\n"
500"\n"
501"Line breaks are not included in the resulting list unless keepends is given and\n"
502"true.");
503
504#define BYTES_SPLITLINES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200505 {"splitlines", (PyCFunction)(void(*)(void))bytes_splitlines, METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300506
507static PyObject *
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300508bytes_splitlines_impl(PyBytesObject *self, int keepends);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300509
510static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200511bytes_splitlines(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300512{
513 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300514 static const char * const _keywords[] = {"keepends", NULL};
515 static _PyArg_Parser _parser = {"|i:splitlines", _keywords, 0};
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300516 int keepends = 0;
517
Victor Stinner3e1fad62017-01-17 01:29:01 +0100518 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300519 &keepends)) {
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300520 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300521 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300522 return_value = bytes_splitlines_impl(self, keepends);
523
524exit:
525 return return_value;
526}
527
528PyDoc_STRVAR(bytes_fromhex__doc__,
529"fromhex($type, string, /)\n"
530"--\n"
531"\n"
532"Create a bytes object from a string of hexadecimal numbers.\n"
533"\n"
534"Spaces between two numbers are accepted.\n"
535"Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'.");
536
537#define BYTES_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300538 {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300539
540static PyObject *
541bytes_fromhex_impl(PyTypeObject *type, PyObject *string);
542
543static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300544bytes_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300545{
546 PyObject *return_value = NULL;
547 PyObject *string;
548
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200549 if (!PyUnicode_Check(arg)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200550 _PyArg_BadArgument("fromhex", 0, "str", arg);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300551 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300552 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200553 if (PyUnicode_READY(arg) == -1) {
554 goto exit;
555 }
556 string = arg;
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300557 return_value = bytes_fromhex_impl(type, string);
558
559exit:
560 return return_value;
561}
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200562/*[clinic end generated code: output=810c8dfc72520ca4 input=a9049054013a1b77]*/