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