/src/Python-3.8.3/Objects/clinic/bytesobject.c.h
Line | Count | Source (jump to first uncovered line) |
1 | | /*[clinic input] |
2 | | preserve |
3 | | [clinic start generated code]*/ |
4 | | |
5 | | PyDoc_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)(void(*)(void))bytes_split, METH_FASTCALL|METH_KEYWORDS, bytes_split__doc__}, |
21 | | |
22 | | static PyObject * |
23 | | bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit); |
24 | | |
25 | | static PyObject * |
26 | | bytes_split(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
27 | 155 | { |
28 | 155 | PyObject *return_value = NULL; |
29 | 155 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
30 | 155 | static _PyArg_Parser _parser = {NULL, _keywords, "split", 0}; |
31 | 155 | PyObject *argsbuf[2]; |
32 | 155 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
33 | 155 | PyObject *sep = Py_None; |
34 | 155 | Py_ssize_t maxsplit = -1; |
35 | | |
36 | 155 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
37 | 155 | if (!args) { |
38 | 0 | goto exit; |
39 | 0 | } |
40 | 155 | if (!noptargs) { |
41 | 0 | goto skip_optional_pos; |
42 | 0 | } |
43 | 155 | if (args[0]) { |
44 | 155 | sep = args[0]; |
45 | 155 | if (!--noptargs) { |
46 | 77 | goto skip_optional_pos; |
47 | 77 | } |
48 | 155 | } |
49 | 78 | if (PyFloat_Check(args[1])) { |
50 | 0 | PyErr_SetString(PyExc_TypeError, |
51 | 0 | "integer argument expected, got float" ); |
52 | 0 | goto exit; |
53 | 0 | } |
54 | 78 | { |
55 | 78 | Py_ssize_t ival = -1; |
56 | 78 | PyObject *iobj = PyNumber_Index(args[1]); |
57 | 78 | if (iobj != NULL) { |
58 | 78 | ival = PyLong_AsSsize_t(iobj); |
59 | 78 | Py_DECREF(iobj); |
60 | 78 | } |
61 | 78 | if (ival == -1 && PyErr_Occurred()) { |
62 | 0 | goto exit; |
63 | 0 | } |
64 | 78 | maxsplit = ival; |
65 | 78 | } |
66 | 155 | skip_optional_pos: |
67 | 155 | return_value = bytes_split_impl(self, sep, maxsplit); |
68 | | |
69 | 155 | exit: |
70 | 155 | return return_value; |
71 | 155 | } |
72 | | |
73 | | PyDoc_STRVAR(bytes_partition__doc__, |
74 | | "partition($self, sep, /)\n" |
75 | | "--\n" |
76 | | "\n" |
77 | | "Partition the bytes into three parts using the given separator.\n" |
78 | | "\n" |
79 | | "This will search for the separator sep in the bytes. If the separator is found,\n" |
80 | | "returns a 3-tuple containing the part before the separator, the separator\n" |
81 | | "itself, and the part after it.\n" |
82 | | "\n" |
83 | | "If the separator is not found, returns a 3-tuple containing the original bytes\n" |
84 | | "object and two empty bytes objects."); |
85 | | |
86 | | #define BYTES_PARTITION_METHODDEF \ |
87 | | {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__}, |
88 | | |
89 | | static PyObject * |
90 | | bytes_partition_impl(PyBytesObject *self, Py_buffer *sep); |
91 | | |
92 | | static PyObject * |
93 | | bytes_partition(PyBytesObject *self, PyObject *arg) |
94 | 0 | { |
95 | 0 | PyObject *return_value = NULL; |
96 | 0 | Py_buffer sep = {NULL, NULL}; |
97 | |
|
98 | 0 | if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) { |
99 | 0 | goto exit; |
100 | 0 | } |
101 | 0 | if (!PyBuffer_IsContiguous(&sep, 'C')) { |
102 | 0 | _PyArg_BadArgument("partition", "argument", "contiguous buffer", arg); |
103 | 0 | goto exit; |
104 | 0 | } |
105 | 0 | return_value = bytes_partition_impl(self, &sep); |
106 | |
|
107 | 0 | exit: |
108 | | /* Cleanup for sep */ |
109 | 0 | if (sep.obj) { |
110 | 0 | PyBuffer_Release(&sep); |
111 | 0 | } |
112 | |
|
113 | 0 | return return_value; |
114 | 0 | } |
115 | | |
116 | | PyDoc_STRVAR(bytes_rpartition__doc__, |
117 | | "rpartition($self, sep, /)\n" |
118 | | "--\n" |
119 | | "\n" |
120 | | "Partition the bytes into three parts using the given separator.\n" |
121 | | "\n" |
122 | | "This will search for the separator sep in the bytes, starting at the end. If\n" |
123 | | "the separator is found, returns a 3-tuple containing the part before the\n" |
124 | | "separator, the separator itself, and the part after it.\n" |
125 | | "\n" |
126 | | "If the separator is not found, returns a 3-tuple containing two empty bytes\n" |
127 | | "objects and the original bytes object."); |
128 | | |
129 | | #define BYTES_RPARTITION_METHODDEF \ |
130 | | {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__}, |
131 | | |
132 | | static PyObject * |
133 | | bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep); |
134 | | |
135 | | static PyObject * |
136 | | bytes_rpartition(PyBytesObject *self, PyObject *arg) |
137 | 0 | { |
138 | 0 | PyObject *return_value = NULL; |
139 | 0 | Py_buffer sep = {NULL, NULL}; |
140 | |
|
141 | 0 | if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) { |
142 | 0 | goto exit; |
143 | 0 | } |
144 | 0 | if (!PyBuffer_IsContiguous(&sep, 'C')) { |
145 | 0 | _PyArg_BadArgument("rpartition", "argument", "contiguous buffer", arg); |
146 | 0 | goto exit; |
147 | 0 | } |
148 | 0 | return_value = bytes_rpartition_impl(self, &sep); |
149 | |
|
150 | 0 | exit: |
151 | | /* Cleanup for sep */ |
152 | 0 | if (sep.obj) { |
153 | 0 | PyBuffer_Release(&sep); |
154 | 0 | } |
155 | |
|
156 | 0 | return return_value; |
157 | 0 | } |
158 | | |
159 | | PyDoc_STRVAR(bytes_rsplit__doc__, |
160 | | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
161 | | "--\n" |
162 | | "\n" |
163 | | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
164 | | "\n" |
165 | | " sep\n" |
166 | | " The delimiter according which to split the bytes.\n" |
167 | | " None (the default value) means split on ASCII whitespace characters\n" |
168 | | " (space, tab, return, newline, formfeed, vertical tab).\n" |
169 | | " maxsplit\n" |
170 | | " Maximum number of splits to do.\n" |
171 | | " -1 (the default value) means no limit.\n" |
172 | | "\n" |
173 | | "Splitting is done starting at the end of the bytes and working to the front."); |
174 | | |
175 | | #define BYTES_RSPLIT_METHODDEF \ |
176 | | {"rsplit", (PyCFunction)(void(*)(void))bytes_rsplit, METH_FASTCALL|METH_KEYWORDS, bytes_rsplit__doc__}, |
177 | | |
178 | | static PyObject * |
179 | | bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit); |
180 | | |
181 | | static PyObject * |
182 | | bytes_rsplit(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
183 | 0 | { |
184 | 0 | PyObject *return_value = NULL; |
185 | 0 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
186 | 0 | static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0}; |
187 | 0 | PyObject *argsbuf[2]; |
188 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
189 | 0 | PyObject *sep = Py_None; |
190 | 0 | Py_ssize_t maxsplit = -1; |
191 | |
|
192 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
193 | 0 | if (!args) { |
194 | 0 | goto exit; |
195 | 0 | } |
196 | 0 | if (!noptargs) { |
197 | 0 | goto skip_optional_pos; |
198 | 0 | } |
199 | 0 | if (args[0]) { |
200 | 0 | sep = args[0]; |
201 | 0 | if (!--noptargs) { |
202 | 0 | goto skip_optional_pos; |
203 | 0 | } |
204 | 0 | } |
205 | 0 | if (PyFloat_Check(args[1])) { |
206 | 0 | PyErr_SetString(PyExc_TypeError, |
207 | 0 | "integer argument expected, got float" ); |
208 | 0 | goto exit; |
209 | 0 | } |
210 | 0 | { |
211 | 0 | Py_ssize_t ival = -1; |
212 | 0 | PyObject *iobj = PyNumber_Index(args[1]); |
213 | 0 | if (iobj != NULL) { |
214 | 0 | ival = PyLong_AsSsize_t(iobj); |
215 | 0 | Py_DECREF(iobj); |
216 | 0 | } |
217 | 0 | if (ival == -1 && PyErr_Occurred()) { |
218 | 0 | goto exit; |
219 | 0 | } |
220 | 0 | maxsplit = ival; |
221 | 0 | } |
222 | 0 | skip_optional_pos: |
223 | 0 | return_value = bytes_rsplit_impl(self, sep, maxsplit); |
224 | |
|
225 | 0 | exit: |
226 | 0 | return return_value; |
227 | 0 | } |
228 | | |
229 | | PyDoc_STRVAR(bytes_join__doc__, |
230 | | "join($self, iterable_of_bytes, /)\n" |
231 | | "--\n" |
232 | | "\n" |
233 | | "Concatenate any number of bytes objects.\n" |
234 | | "\n" |
235 | | "The bytes whose method is called is inserted in between each pair.\n" |
236 | | "\n" |
237 | | "The result is returned as a new bytes object.\n" |
238 | | "\n" |
239 | | "Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'."); |
240 | | |
241 | | #define BYTES_JOIN_METHODDEF \ |
242 | | {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__}, |
243 | | |
244 | | PyDoc_STRVAR(bytes_strip__doc__, |
245 | | "strip($self, bytes=None, /)\n" |
246 | | "--\n" |
247 | | "\n" |
248 | | "Strip leading and trailing bytes contained in the argument.\n" |
249 | | "\n" |
250 | | "If the argument is omitted or None, strip leading and trailing ASCII whitespace."); |
251 | | |
252 | | #define BYTES_STRIP_METHODDEF \ |
253 | | {"strip", (PyCFunction)(void(*)(void))bytes_strip, METH_FASTCALL, bytes_strip__doc__}, |
254 | | |
255 | | static PyObject * |
256 | | bytes_strip_impl(PyBytesObject *self, PyObject *bytes); |
257 | | |
258 | | static PyObject * |
259 | | bytes_strip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
260 | 0 | { |
261 | 0 | PyObject *return_value = NULL; |
262 | 0 | PyObject *bytes = Py_None; |
263 | |
|
264 | 0 | if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) { |
265 | 0 | goto exit; |
266 | 0 | } |
267 | 0 | if (nargs < 1) { |
268 | 0 | goto skip_optional; |
269 | 0 | } |
270 | 0 | bytes = args[0]; |
271 | 0 | skip_optional: |
272 | 0 | return_value = bytes_strip_impl(self, bytes); |
273 | |
|
274 | 0 | exit: |
275 | 0 | return return_value; |
276 | 0 | } |
277 | | |
278 | | PyDoc_STRVAR(bytes_lstrip__doc__, |
279 | | "lstrip($self, bytes=None, /)\n" |
280 | | "--\n" |
281 | | "\n" |
282 | | "Strip leading bytes contained in the argument.\n" |
283 | | "\n" |
284 | | "If the argument is omitted or None, strip leading ASCII whitespace."); |
285 | | |
286 | | #define BYTES_LSTRIP_METHODDEF \ |
287 | | {"lstrip", (PyCFunction)(void(*)(void))bytes_lstrip, METH_FASTCALL, bytes_lstrip__doc__}, |
288 | | |
289 | | static PyObject * |
290 | | bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes); |
291 | | |
292 | | static PyObject * |
293 | | bytes_lstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
294 | 0 | { |
295 | 0 | PyObject *return_value = NULL; |
296 | 0 | PyObject *bytes = Py_None; |
297 | |
|
298 | 0 | if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) { |
299 | 0 | goto exit; |
300 | 0 | } |
301 | 0 | if (nargs < 1) { |
302 | 0 | goto skip_optional; |
303 | 0 | } |
304 | 0 | bytes = args[0]; |
305 | 0 | skip_optional: |
306 | 0 | return_value = bytes_lstrip_impl(self, bytes); |
307 | |
|
308 | 0 | exit: |
309 | 0 | return return_value; |
310 | 0 | } |
311 | | |
312 | | PyDoc_STRVAR(bytes_rstrip__doc__, |
313 | | "rstrip($self, bytes=None, /)\n" |
314 | | "--\n" |
315 | | "\n" |
316 | | "Strip trailing bytes contained in the argument.\n" |
317 | | "\n" |
318 | | "If the argument is omitted or None, strip trailing ASCII whitespace."); |
319 | | |
320 | | #define BYTES_RSTRIP_METHODDEF \ |
321 | | {"rstrip", (PyCFunction)(void(*)(void))bytes_rstrip, METH_FASTCALL, bytes_rstrip__doc__}, |
322 | | |
323 | | static PyObject * |
324 | | bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes); |
325 | | |
326 | | static PyObject * |
327 | | bytes_rstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
328 | 0 | { |
329 | 0 | PyObject *return_value = NULL; |
330 | 0 | PyObject *bytes = Py_None; |
331 | |
|
332 | 0 | if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) { |
333 | 0 | goto exit; |
334 | 0 | } |
335 | 0 | if (nargs < 1) { |
336 | 0 | goto skip_optional; |
337 | 0 | } |
338 | 0 | bytes = args[0]; |
339 | 0 | skip_optional: |
340 | 0 | return_value = bytes_rstrip_impl(self, bytes); |
341 | |
|
342 | 0 | exit: |
343 | 0 | return return_value; |
344 | 0 | } |
345 | | |
346 | | PyDoc_STRVAR(bytes_translate__doc__, |
347 | | "translate($self, table, /, delete=b\'\')\n" |
348 | | "--\n" |
349 | | "\n" |
350 | | "Return a copy with each character mapped by the given translation table.\n" |
351 | | "\n" |
352 | | " table\n" |
353 | | " Translation table, which must be a bytes object of length 256.\n" |
354 | | "\n" |
355 | | "All characters occurring in the optional argument delete are removed.\n" |
356 | | "The remaining characters are mapped through the given translation table."); |
357 | | |
358 | | #define BYTES_TRANSLATE_METHODDEF \ |
359 | | {"translate", (PyCFunction)(void(*)(void))bytes_translate, METH_FASTCALL|METH_KEYWORDS, bytes_translate__doc__}, |
360 | | |
361 | | static PyObject * |
362 | | bytes_translate_impl(PyBytesObject *self, PyObject *table, |
363 | | PyObject *deletechars); |
364 | | |
365 | | static PyObject * |
366 | | bytes_translate(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
367 | 0 | { |
368 | 0 | PyObject *return_value = NULL; |
369 | 0 | static const char * const _keywords[] = {"", "delete", NULL}; |
370 | 0 | static _PyArg_Parser _parser = {NULL, _keywords, "translate", 0}; |
371 | 0 | PyObject *argsbuf[2]; |
372 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
373 | 0 | PyObject *table; |
374 | 0 | PyObject *deletechars = NULL; |
375 | |
|
376 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); |
377 | 0 | if (!args) { |
378 | 0 | goto exit; |
379 | 0 | } |
380 | 0 | table = args[0]; |
381 | 0 | if (!noptargs) { |
382 | 0 | goto skip_optional_pos; |
383 | 0 | } |
384 | 0 | deletechars = args[1]; |
385 | 0 | skip_optional_pos: |
386 | 0 | return_value = bytes_translate_impl(self, table, deletechars); |
387 | |
|
388 | 0 | exit: |
389 | 0 | return return_value; |
390 | 0 | } |
391 | | |
392 | | PyDoc_STRVAR(bytes_maketrans__doc__, |
393 | | "maketrans(frm, to, /)\n" |
394 | | "--\n" |
395 | | "\n" |
396 | | "Return a translation table useable for the bytes or bytearray translate method.\n" |
397 | | "\n" |
398 | | "The returned table will be one where each byte in frm is mapped to the byte at\n" |
399 | | "the same position in to.\n" |
400 | | "\n" |
401 | | "The bytes objects frm and to must be of the same length."); |
402 | | |
403 | | #define BYTES_MAKETRANS_METHODDEF \ |
404 | | {"maketrans", (PyCFunction)(void(*)(void))bytes_maketrans, METH_FASTCALL|METH_STATIC, bytes_maketrans__doc__}, |
405 | | |
406 | | static PyObject * |
407 | | bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to); |
408 | | |
409 | | static PyObject * |
410 | | bytes_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs) |
411 | 0 | { |
412 | 0 | PyObject *return_value = NULL; |
413 | 0 | Py_buffer frm = {NULL, NULL}; |
414 | 0 | Py_buffer to = {NULL, NULL}; |
415 | |
|
416 | 0 | if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) { |
417 | 0 | goto exit; |
418 | 0 | } |
419 | 0 | if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) { |
420 | 0 | goto exit; |
421 | 0 | } |
422 | 0 | if (!PyBuffer_IsContiguous(&frm, 'C')) { |
423 | 0 | _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]); |
424 | 0 | goto exit; |
425 | 0 | } |
426 | 0 | if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) { |
427 | 0 | goto exit; |
428 | 0 | } |
429 | 0 | if (!PyBuffer_IsContiguous(&to, 'C')) { |
430 | 0 | _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]); |
431 | 0 | goto exit; |
432 | 0 | } |
433 | 0 | return_value = bytes_maketrans_impl(&frm, &to); |
434 | |
|
435 | 0 | exit: |
436 | | /* Cleanup for frm */ |
437 | 0 | if (frm.obj) { |
438 | 0 | PyBuffer_Release(&frm); |
439 | 0 | } |
440 | | /* Cleanup for to */ |
441 | 0 | if (to.obj) { |
442 | 0 | PyBuffer_Release(&to); |
443 | 0 | } |
444 | |
|
445 | 0 | return return_value; |
446 | 0 | } |
447 | | |
448 | | PyDoc_STRVAR(bytes_replace__doc__, |
449 | | "replace($self, old, new, count=-1, /)\n" |
450 | | "--\n" |
451 | | "\n" |
452 | | "Return a copy with all occurrences of substring old replaced by new.\n" |
453 | | "\n" |
454 | | " count\n" |
455 | | " Maximum number of occurrences to replace.\n" |
456 | | " -1 (the default value) means replace all occurrences.\n" |
457 | | "\n" |
458 | | "If the optional argument count is given, only the first count occurrences are\n" |
459 | | "replaced."); |
460 | | |
461 | | #define BYTES_REPLACE_METHODDEF \ |
462 | | {"replace", (PyCFunction)(void(*)(void))bytes_replace, METH_FASTCALL, bytes_replace__doc__}, |
463 | | |
464 | | static PyObject * |
465 | | bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new, |
466 | | Py_ssize_t count); |
467 | | |
468 | | static PyObject * |
469 | | bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
470 | 0 | { |
471 | 0 | PyObject *return_value = NULL; |
472 | 0 | Py_buffer old = {NULL, NULL}; |
473 | 0 | Py_buffer new = {NULL, NULL}; |
474 | 0 | Py_ssize_t count = -1; |
475 | |
|
476 | 0 | if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) { |
477 | 0 | goto exit; |
478 | 0 | } |
479 | 0 | if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) { |
480 | 0 | goto exit; |
481 | 0 | } |
482 | 0 | if (!PyBuffer_IsContiguous(&old, 'C')) { |
483 | 0 | _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]); |
484 | 0 | goto exit; |
485 | 0 | } |
486 | 0 | if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) { |
487 | 0 | goto exit; |
488 | 0 | } |
489 | 0 | if (!PyBuffer_IsContiguous(&new, 'C')) { |
490 | 0 | _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]); |
491 | 0 | goto exit; |
492 | 0 | } |
493 | 0 | if (nargs < 3) { |
494 | 0 | goto skip_optional; |
495 | 0 | } |
496 | 0 | if (PyFloat_Check(args[2])) { |
497 | 0 | PyErr_SetString(PyExc_TypeError, |
498 | 0 | "integer argument expected, got float" ); |
499 | 0 | goto exit; |
500 | 0 | } |
501 | 0 | { |
502 | 0 | Py_ssize_t ival = -1; |
503 | 0 | PyObject *iobj = PyNumber_Index(args[2]); |
504 | 0 | if (iobj != NULL) { |
505 | 0 | ival = PyLong_AsSsize_t(iobj); |
506 | 0 | Py_DECREF(iobj); |
507 | 0 | } |
508 | 0 | if (ival == -1 && PyErr_Occurred()) { |
509 | 0 | goto exit; |
510 | 0 | } |
511 | 0 | count = ival; |
512 | 0 | } |
513 | 0 | skip_optional: |
514 | 0 | return_value = bytes_replace_impl(self, &old, &new, count); |
515 | |
|
516 | 0 | exit: |
517 | | /* Cleanup for old */ |
518 | 0 | if (old.obj) { |
519 | 0 | PyBuffer_Release(&old); |
520 | 0 | } |
521 | | /* Cleanup for new */ |
522 | 0 | if (new.obj) { |
523 | 0 | PyBuffer_Release(&new); |
524 | 0 | } |
525 | |
|
526 | 0 | return return_value; |
527 | 0 | } |
528 | | |
529 | | PyDoc_STRVAR(bytes_decode__doc__, |
530 | | "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
531 | | "--\n" |
532 | | "\n" |
533 | | "Decode the bytes using the codec registered for encoding.\n" |
534 | | "\n" |
535 | | " encoding\n" |
536 | | " The encoding with which to decode the bytes.\n" |
537 | | " errors\n" |
538 | | " The error handling scheme to use for the handling of decoding errors.\n" |
539 | | " The default is \'strict\' meaning that decoding errors raise a\n" |
540 | | " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n" |
541 | | " as well as any other name registered with codecs.register_error that\n" |
542 | | " can handle UnicodeDecodeErrors."); |
543 | | |
544 | | #define BYTES_DECODE_METHODDEF \ |
545 | | {"decode", (PyCFunction)(void(*)(void))bytes_decode, METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__}, |
546 | | |
547 | | static PyObject * |
548 | | bytes_decode_impl(PyBytesObject *self, const char *encoding, |
549 | | const char *errors); |
550 | | |
551 | | static PyObject * |
552 | | bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
553 | 16 | { |
554 | 16 | PyObject *return_value = NULL; |
555 | 16 | static const char * const _keywords[] = {"encoding", "errors", NULL}; |
556 | 16 | static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0}; |
557 | 16 | PyObject *argsbuf[2]; |
558 | 16 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
559 | 16 | const char *encoding = NULL; |
560 | 16 | const char *errors = NULL; |
561 | | |
562 | 16 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
563 | 16 | if (!args) { |
564 | 0 | goto exit; |
565 | 0 | } |
566 | 16 | if (!noptargs) { |
567 | 0 | goto skip_optional_pos; |
568 | 0 | } |
569 | 16 | if (args[0]) { |
570 | 16 | if (!PyUnicode_Check(args[0])) { |
571 | 0 | _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]); |
572 | 0 | goto exit; |
573 | 0 | } |
574 | 16 | Py_ssize_t encoding_length; |
575 | 16 | encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length); |
576 | 16 | if (encoding == NULL) { |
577 | 0 | goto exit; |
578 | 0 | } |
579 | 16 | if (strlen(encoding) != (size_t)encoding_length) { |
580 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
581 | 0 | goto exit; |
582 | 0 | } |
583 | 16 | if (!--noptargs) { |
584 | 2 | goto skip_optional_pos; |
585 | 2 | } |
586 | 16 | } |
587 | 14 | if (!PyUnicode_Check(args[1])) { |
588 | 0 | _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]); |
589 | 0 | goto exit; |
590 | 0 | } |
591 | 14 | Py_ssize_t errors_length; |
592 | 14 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
593 | 14 | if (errors == NULL) { |
594 | 0 | goto exit; |
595 | 0 | } |
596 | 14 | if (strlen(errors) != (size_t)errors_length) { |
597 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
598 | 0 | goto exit; |
599 | 0 | } |
600 | 16 | skip_optional_pos: |
601 | 16 | return_value = bytes_decode_impl(self, encoding, errors); |
602 | | |
603 | 16 | exit: |
604 | 16 | return return_value; |
605 | 16 | } |
606 | | |
607 | | PyDoc_STRVAR(bytes_splitlines__doc__, |
608 | | "splitlines($self, /, keepends=False)\n" |
609 | | "--\n" |
610 | | "\n" |
611 | | "Return a list of the lines in the bytes, breaking at line boundaries.\n" |
612 | | "\n" |
613 | | "Line breaks are not included in the resulting list unless keepends is given and\n" |
614 | | "true."); |
615 | | |
616 | | #define BYTES_SPLITLINES_METHODDEF \ |
617 | | {"splitlines", (PyCFunction)(void(*)(void))bytes_splitlines, METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__}, |
618 | | |
619 | | static PyObject * |
620 | | bytes_splitlines_impl(PyBytesObject *self, int keepends); |
621 | | |
622 | | static PyObject * |
623 | | bytes_splitlines(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
624 | 0 | { |
625 | 0 | PyObject *return_value = NULL; |
626 | 0 | static const char * const _keywords[] = {"keepends", NULL}; |
627 | 0 | static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0}; |
628 | 0 | PyObject *argsbuf[1]; |
629 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
630 | 0 | int keepends = 0; |
631 | |
|
632 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf); |
633 | 0 | if (!args) { |
634 | 0 | goto exit; |
635 | 0 | } |
636 | 0 | if (!noptargs) { |
637 | 0 | goto skip_optional_pos; |
638 | 0 | } |
639 | 0 | if (PyFloat_Check(args[0])) { |
640 | 0 | PyErr_SetString(PyExc_TypeError, |
641 | 0 | "integer argument expected, got float" ); |
642 | 0 | goto exit; |
643 | 0 | } |
644 | 0 | keepends = _PyLong_AsInt(args[0]); |
645 | 0 | if (keepends == -1 && PyErr_Occurred()) { |
646 | 0 | goto exit; |
647 | 0 | } |
648 | 0 | skip_optional_pos: |
649 | 0 | return_value = bytes_splitlines_impl(self, keepends); |
650 | |
|
651 | 0 | exit: |
652 | 0 | return return_value; |
653 | 0 | } |
654 | | |
655 | | PyDoc_STRVAR(bytes_fromhex__doc__, |
656 | | "fromhex($type, string, /)\n" |
657 | | "--\n" |
658 | | "\n" |
659 | | "Create a bytes object from a string of hexadecimal numbers.\n" |
660 | | "\n" |
661 | | "Spaces between two numbers are accepted.\n" |
662 | | "Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'."); |
663 | | |
664 | | #define BYTES_FROMHEX_METHODDEF \ |
665 | | {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__}, |
666 | | |
667 | | static PyObject * |
668 | | bytes_fromhex_impl(PyTypeObject *type, PyObject *string); |
669 | | |
670 | | static PyObject * |
671 | | bytes_fromhex(PyTypeObject *type, PyObject *arg) |
672 | 0 | { |
673 | 0 | PyObject *return_value = NULL; |
674 | 0 | PyObject *string; |
675 | |
|
676 | 0 | if (!PyUnicode_Check(arg)) { |
677 | 0 | _PyArg_BadArgument("fromhex", "argument", "str", arg); |
678 | 0 | goto exit; |
679 | 0 | } |
680 | 0 | if (PyUnicode_READY(arg) == -1) { |
681 | 0 | goto exit; |
682 | 0 | } |
683 | 0 | string = arg; |
684 | 0 | return_value = bytes_fromhex_impl(type, string); |
685 | |
|
686 | 0 | exit: |
687 | 0 | return return_value; |
688 | 0 | } |
689 | | |
690 | | PyDoc_STRVAR(bytes_hex__doc__, |
691 | | "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n" |
692 | | "--\n" |
693 | | "\n" |
694 | | "Create a str of hexadecimal numbers from a bytes object.\n" |
695 | | "\n" |
696 | | " sep\n" |
697 | | " An optional single character or byte to separate hex bytes.\n" |
698 | | " bytes_per_sep\n" |
699 | | " How many bytes between separators. Positive values count from the\n" |
700 | | " right, negative values count from the left.\n" |
701 | | "\n" |
702 | | "Example:\n" |
703 | | ">>> value = b\'\\xb9\\x01\\xef\'\n" |
704 | | ">>> value.hex()\n" |
705 | | "\'b901ef\'\n" |
706 | | ">>> value.hex(\':\')\n" |
707 | | "\'b9:01:ef\'\n" |
708 | | ">>> value.hex(\':\', 2)\n" |
709 | | "\'b9:01ef\'\n" |
710 | | ">>> value.hex(\':\', -2)\n" |
711 | | "\'b901:ef\'"); |
712 | | |
713 | | #define BYTES_HEX_METHODDEF \ |
714 | | {"hex", (PyCFunction)(void(*)(void))bytes_hex, METH_FASTCALL|METH_KEYWORDS, bytes_hex__doc__}, |
715 | | |
716 | | static PyObject * |
717 | | bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep); |
718 | | |
719 | | static PyObject * |
720 | | bytes_hex(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
721 | 0 | { |
722 | 0 | PyObject *return_value = NULL; |
723 | 0 | static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL}; |
724 | 0 | static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0}; |
725 | 0 | PyObject *argsbuf[2]; |
726 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
727 | 0 | PyObject *sep = NULL; |
728 | 0 | int bytes_per_sep = 1; |
729 | |
|
730 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
731 | 0 | if (!args) { |
732 | 0 | goto exit; |
733 | 0 | } |
734 | 0 | if (!noptargs) { |
735 | 0 | goto skip_optional_pos; |
736 | 0 | } |
737 | 0 | if (args[0]) { |
738 | 0 | sep = args[0]; |
739 | 0 | if (!--noptargs) { |
740 | 0 | goto skip_optional_pos; |
741 | 0 | } |
742 | 0 | } |
743 | 0 | if (PyFloat_Check(args[1])) { |
744 | 0 | PyErr_SetString(PyExc_TypeError, |
745 | 0 | "integer argument expected, got float" ); |
746 | 0 | goto exit; |
747 | 0 | } |
748 | 0 | bytes_per_sep = _PyLong_AsInt(args[1]); |
749 | 0 | if (bytes_per_sep == -1 && PyErr_Occurred()) { |
750 | 0 | goto exit; |
751 | 0 | } |
752 | 0 | skip_optional_pos: |
753 | 0 | return_value = bytes_hex_impl(self, sep, bytes_per_sep); |
754 | |
|
755 | 0 | exit: |
756 | 0 | return return_value; |
757 | 0 | } |
758 | | /*[clinic end generated code: output=ca60dfccf8d51e88 input=a9049054013a1b77]*/ |