blob: 5a1a5e91a6a2df5f2ce464f08594e4c90c3f0255 [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 \
20 {"split", (PyCFunction)bytes_split, METH_VARARGS|METH_KEYWORDS, bytes_split__doc__},
21
22static PyObject *
23bytes_split_impl(PyBytesObject*self, PyObject *sep, Py_ssize_t maxsplit);
24
25static PyObject *
26bytes_split(PyBytesObject*self, PyObject *args, PyObject *kwargs)
27{
28 PyObject *return_value = NULL;
29 static char *_keywords[] = {"sep", "maxsplit", NULL};
30 PyObject *sep = Py_None;
31 Py_ssize_t maxsplit = -1;
32
Serhiy Storchaka247789c2015-04-24 00:40:51 +030033 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|On:split", _keywords,
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030034 &sep, &maxsplit))
35 goto exit;
36 return_value = bytes_split_impl(self, sep, maxsplit);
37
38exit:
39 return return_value;
40}
41
42PyDoc_STRVAR(bytes_partition__doc__,
43"partition($self, sep, /)\n"
44"--\n"
45"\n"
46"Partition the bytes into three parts using the given separator.\n"
47"\n"
48"This will search for the separator sep in the bytes. If the separator is found,\n"
49"returns a 3-tuple containing the part before the separator, the separator\n"
50"itself, and the part after it.\n"
51"\n"
52"If the separator is not found, returns a 3-tuple containing the original bytes\n"
53"object and two empty bytes objects.");
54
55#define BYTES_PARTITION_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030056 {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030057
58static PyObject *
59bytes_partition_impl(PyBytesObject *self, Py_buffer *sep);
60
61static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030062bytes_partition(PyBytesObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030063{
64 PyObject *return_value = NULL;
65 Py_buffer sep = {NULL, NULL};
66
Serhiy Storchaka247789c2015-04-24 00:40:51 +030067 if (!PyArg_Parse(arg, "y*:partition", &sep))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030068 goto exit;
69 return_value = bytes_partition_impl(self, &sep);
70
71exit:
72 /* Cleanup for sep */
73 if (sep.obj)
74 PyBuffer_Release(&sep);
75
76 return return_value;
77}
78
79PyDoc_STRVAR(bytes_rpartition__doc__,
80"rpartition($self, sep, /)\n"
81"--\n"
82"\n"
83"Partition the bytes into three parts using the given separator.\n"
84"\n"
85"This will search for the separator sep in the bytes, starting and the end. If\n"
86"the separator is found, returns a 3-tuple containing the part before the\n"
87"separator, the separator itself, and the part after it.\n"
88"\n"
89"If the separator is not found, returns a 3-tuple containing two empty bytes\n"
90"objects and the original bytes object.");
91
92#define BYTES_RPARTITION_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030093 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030094
95static PyObject *
96bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep);
97
98static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030099bytes_rpartition(PyBytesObject *self, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300100{
101 PyObject *return_value = NULL;
102 Py_buffer sep = {NULL, NULL};
103
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300104 if (!PyArg_Parse(arg, "y*:rpartition", &sep))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300105 goto exit;
106 return_value = bytes_rpartition_impl(self, &sep);
107
108exit:
109 /* Cleanup for sep */
110 if (sep.obj)
111 PyBuffer_Release(&sep);
112
113 return return_value;
114}
115
116PyDoc_STRVAR(bytes_rsplit__doc__,
117"rsplit($self, /, sep=None, maxsplit=-1)\n"
118"--\n"
119"\n"
120"Return a list of the sections in the bytes, using sep as the delimiter.\n"
121"\n"
122" sep\n"
123" The delimiter according which to split the bytes.\n"
124" None (the default value) means split on ASCII whitespace characters\n"
125" (space, tab, return, newline, formfeed, vertical tab).\n"
126" maxsplit\n"
127" Maximum number of splits to do.\n"
128" -1 (the default value) means no limit.\n"
129"\n"
130"Splitting is done starting at the end of the bytes and working to the front.");
131
132#define BYTES_RSPLIT_METHODDEF \
133 {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS|METH_KEYWORDS, bytes_rsplit__doc__},
134
135static PyObject *
136bytes_rsplit_impl(PyBytesObject*self, PyObject *sep, Py_ssize_t maxsplit);
137
138static PyObject *
139bytes_rsplit(PyBytesObject*self, PyObject *args, PyObject *kwargs)
140{
141 PyObject *return_value = NULL;
142 static char *_keywords[] = {"sep", "maxsplit", NULL};
143 PyObject *sep = Py_None;
144 Py_ssize_t maxsplit = -1;
145
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300146 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|On:rsplit", _keywords,
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300147 &sep, &maxsplit))
148 goto exit;
149 return_value = bytes_rsplit_impl(self, sep, maxsplit);
150
151exit:
152 return return_value;
153}
154
155PyDoc_STRVAR(bytes_join__doc__,
156"join($self, iterable_of_bytes, /)\n"
157"--\n"
158"\n"
159"Concatenate any number of bytes objects.\n"
160"\n"
161"The bytes whose method is called is inserted in between each pair.\n"
162"\n"
163"The result is returned as a new bytes object.\n"
164"\n"
165"Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'.");
166
167#define BYTES_JOIN_METHODDEF \
168 {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__},
169
170PyDoc_STRVAR(bytes_strip__doc__,
171"strip($self, bytes=None, /)\n"
172"--\n"
173"\n"
174"Strip leading and trailing bytes contained in the argument.\n"
175"\n"
176"If the argument is omitted or None, strip leading and trailing ASCII whitespace.");
177
178#define BYTES_STRIP_METHODDEF \
179 {"strip", (PyCFunction)bytes_strip, METH_VARARGS, bytes_strip__doc__},
180
181static PyObject *
182bytes_strip_impl(PyBytesObject *self, PyObject *bytes);
183
184static PyObject *
185bytes_strip(PyBytesObject *self, PyObject *args)
186{
187 PyObject *return_value = NULL;
188 PyObject *bytes = Py_None;
189
190 if (!PyArg_UnpackTuple(args, "strip",
191 0, 1,
192 &bytes))
193 goto exit;
194 return_value = bytes_strip_impl(self, bytes);
195
196exit:
197 return return_value;
198}
199
200PyDoc_STRVAR(bytes_lstrip__doc__,
201"lstrip($self, bytes=None, /)\n"
202"--\n"
203"\n"
204"Strip leading bytes contained in the argument.\n"
205"\n"
206"If the argument is omitted or None, strip leading ASCII whitespace.");
207
208#define BYTES_LSTRIP_METHODDEF \
209 {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, bytes_lstrip__doc__},
210
211static PyObject *
212bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes);
213
214static PyObject *
215bytes_lstrip(PyBytesObject *self, PyObject *args)
216{
217 PyObject *return_value = NULL;
218 PyObject *bytes = Py_None;
219
220 if (!PyArg_UnpackTuple(args, "lstrip",
221 0, 1,
222 &bytes))
223 goto exit;
224 return_value = bytes_lstrip_impl(self, bytes);
225
226exit:
227 return return_value;
228}
229
230PyDoc_STRVAR(bytes_rstrip__doc__,
231"rstrip($self, bytes=None, /)\n"
232"--\n"
233"\n"
234"Strip trailing bytes contained in the argument.\n"
235"\n"
236"If the argument is omitted or None, strip trailing ASCII whitespace.");
237
238#define BYTES_RSTRIP_METHODDEF \
239 {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, bytes_rstrip__doc__},
240
241static PyObject *
242bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes);
243
244static PyObject *
245bytes_rstrip(PyBytesObject *self, PyObject *args)
246{
247 PyObject *return_value = NULL;
248 PyObject *bytes = Py_None;
249
250 if (!PyArg_UnpackTuple(args, "rstrip",
251 0, 1,
252 &bytes))
253 goto exit;
254 return_value = bytes_rstrip_impl(self, bytes);
255
256exit:
257 return return_value;
258}
259
260PyDoc_STRVAR(bytes_translate__doc__,
261"translate(table, [deletechars])\n"
262"Return a copy with each character mapped by the given translation table.\n"
263"\n"
264" table\n"
265" Translation table, which must be a bytes object of length 256.\n"
266"\n"
267"All characters occurring in the optional argument deletechars are removed.\n"
268"The remaining characters are mapped through the given translation table.");
269
270#define BYTES_TRANSLATE_METHODDEF \
271 {"translate", (PyCFunction)bytes_translate, METH_VARARGS, bytes_translate__doc__},
272
273static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400274bytes_translate_impl(PyBytesObject *self, PyObject *table, int group_right_1,
275 PyObject *deletechars);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300276
277static PyObject *
278bytes_translate(PyBytesObject *self, PyObject *args)
279{
280 PyObject *return_value = NULL;
281 PyObject *table;
282 int group_right_1 = 0;
283 PyObject *deletechars = NULL;
284
285 switch (PyTuple_GET_SIZE(args)) {
286 case 1:
287 if (!PyArg_ParseTuple(args, "O:translate", &table))
288 goto exit;
289 break;
290 case 2:
291 if (!PyArg_ParseTuple(args, "OO:translate", &table, &deletechars))
292 goto exit;
293 group_right_1 = 1;
294 break;
295 default:
296 PyErr_SetString(PyExc_TypeError, "bytes.translate requires 1 to 2 arguments");
297 goto exit;
298 }
299 return_value = bytes_translate_impl(self, table, group_right_1, deletechars);
300
301exit:
302 return return_value;
303}
304
305PyDoc_STRVAR(bytes_maketrans__doc__,
306"maketrans(frm, to, /)\n"
307"--\n"
308"\n"
309"Return a translation table useable for the bytes or bytearray translate method.\n"
310"\n"
311"The returned table will be one where each byte in frm is mapped to the byte at\n"
312"the same position in to.\n"
313"\n"
314"The bytes objects frm and to must be of the same length.");
315
316#define BYTES_MAKETRANS_METHODDEF \
317 {"maketrans", (PyCFunction)bytes_maketrans, METH_VARARGS|METH_STATIC, bytes_maketrans__doc__},
318
319static PyObject *
320bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to);
321
322static PyObject *
323bytes_maketrans(void *null, PyObject *args)
324{
325 PyObject *return_value = NULL;
326 Py_buffer frm = {NULL, NULL};
327 Py_buffer to = {NULL, NULL};
328
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300329 if (!PyArg_ParseTuple(args, "y*y*:maketrans",
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300330 &frm, &to))
331 goto exit;
332 return_value = bytes_maketrans_impl(&frm, &to);
333
334exit:
335 /* Cleanup for frm */
336 if (frm.obj)
337 PyBuffer_Release(&frm);
338 /* Cleanup for to */
339 if (to.obj)
340 PyBuffer_Release(&to);
341
342 return return_value;
343}
344
345PyDoc_STRVAR(bytes_replace__doc__,
346"replace($self, old, new, count=-1, /)\n"
347"--\n"
348"\n"
349"Return a copy with all occurrences of substring old replaced by new.\n"
350"\n"
351" count\n"
352" Maximum number of occurrences to replace.\n"
353" -1 (the default value) means replace all occurrences.\n"
354"\n"
355"If the optional argument count is given, only the first count occurrences are\n"
356"replaced.");
357
358#define BYTES_REPLACE_METHODDEF \
359 {"replace", (PyCFunction)bytes_replace, METH_VARARGS, bytes_replace__doc__},
360
361static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400362bytes_replace_impl(PyBytesObject*self, Py_buffer *old, Py_buffer *new,
363 Py_ssize_t count);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300364
365static PyObject *
366bytes_replace(PyBytesObject*self, PyObject *args)
367{
368 PyObject *return_value = NULL;
369 Py_buffer old = {NULL, NULL};
370 Py_buffer new = {NULL, NULL};
371 Py_ssize_t count = -1;
372
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300373 if (!PyArg_ParseTuple(args, "y*y*|n:replace",
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300374 &old, &new, &count))
375 goto exit;
376 return_value = bytes_replace_impl(self, &old, &new, count);
377
378exit:
379 /* Cleanup for old */
380 if (old.obj)
381 PyBuffer_Release(&old);
382 /* Cleanup for new */
383 if (new.obj)
384 PyBuffer_Release(&new);
385
386 return return_value;
387}
388
389PyDoc_STRVAR(bytes_decode__doc__,
390"decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n"
391"--\n"
392"\n"
393"Decode the bytes using the codec registered for encoding.\n"
394"\n"
395" encoding\n"
396" The encoding with which to decode the bytes.\n"
397" errors\n"
398" The error handling scheme to use for the handling of decoding errors.\n"
399" The default is \'strict\' meaning that decoding errors raise a\n"
400" UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n"
401" as well as any other name registered with codecs.register_error that\n"
402" can handle UnicodeDecodeErrors.");
403
404#define BYTES_DECODE_METHODDEF \
405 {"decode", (PyCFunction)bytes_decode, METH_VARARGS|METH_KEYWORDS, bytes_decode__doc__},
406
407static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400408bytes_decode_impl(PyBytesObject*self, const char *encoding,
409 const char *errors);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300410
411static PyObject *
412bytes_decode(PyBytesObject*self, PyObject *args, PyObject *kwargs)
413{
414 PyObject *return_value = NULL;
415 static char *_keywords[] = {"encoding", "errors", NULL};
416 const char *encoding = NULL;
417 const char *errors = NULL;
418
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300419 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", _keywords,
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300420 &encoding, &errors))
421 goto exit;
422 return_value = bytes_decode_impl(self, encoding, errors);
423
424exit:
425 return return_value;
426}
427
428PyDoc_STRVAR(bytes_splitlines__doc__,
429"splitlines($self, /, keepends=False)\n"
430"--\n"
431"\n"
432"Return a list of the lines in the bytes, breaking at line boundaries.\n"
433"\n"
434"Line breaks are not included in the resulting list unless keepends is given and\n"
435"true.");
436
437#define BYTES_SPLITLINES_METHODDEF \
438 {"splitlines", (PyCFunction)bytes_splitlines, METH_VARARGS|METH_KEYWORDS, bytes_splitlines__doc__},
439
440static PyObject *
441bytes_splitlines_impl(PyBytesObject*self, int keepends);
442
443static PyObject *
444bytes_splitlines(PyBytesObject*self, PyObject *args, PyObject *kwargs)
445{
446 PyObject *return_value = NULL;
447 static char *_keywords[] = {"keepends", NULL};
448 int keepends = 0;
449
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300450 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:splitlines", _keywords,
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300451 &keepends))
452 goto exit;
453 return_value = bytes_splitlines_impl(self, keepends);
454
455exit:
456 return return_value;
457}
458
459PyDoc_STRVAR(bytes_fromhex__doc__,
460"fromhex($type, string, /)\n"
461"--\n"
462"\n"
463"Create a bytes object from a string of hexadecimal numbers.\n"
464"\n"
465"Spaces between two numbers are accepted.\n"
466"Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'.");
467
468#define BYTES_FROMHEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300469 {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300470
471static PyObject *
472bytes_fromhex_impl(PyTypeObject *type, PyObject *string);
473
474static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300475bytes_fromhex(PyTypeObject *type, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300476{
477 PyObject *return_value = NULL;
478 PyObject *string;
479
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300480 if (!PyArg_Parse(arg, "U:fromhex", &string))
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300481 goto exit;
482 return_value = bytes_fromhex_impl(type, string);
483
484exit:
485 return return_value;
486}
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300487/*[clinic end generated code: output=bd0ce8f25d7e18f4 input=a9049054013a1b77]*/