/src/cpython/Objects/clinic/bytesobject.c.h
Line | Count | Source |
1 | | /*[clinic input] |
2 | | preserve |
3 | | [clinic start generated code]*/ |
4 | | |
5 | | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
6 | | # include "pycore_gc.h" // PyGC_Head |
7 | | # include "pycore_runtime.h" // _Py_ID() |
8 | | #endif |
9 | | #include "pycore_abstract.h" // _PyNumber_Index() |
10 | | #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() |
11 | | |
12 | | PyDoc_STRVAR(bytes___bytes____doc__, |
13 | | "__bytes__($self, /)\n" |
14 | | "--\n" |
15 | | "\n" |
16 | | "Convert this value to exact type bytes."); |
17 | | |
18 | | #define BYTES___BYTES___METHODDEF \ |
19 | | {"__bytes__", (PyCFunction)bytes___bytes__, METH_NOARGS, bytes___bytes____doc__}, |
20 | | |
21 | | static PyObject * |
22 | | bytes___bytes___impl(PyBytesObject *self); |
23 | | |
24 | | static PyObject * |
25 | | bytes___bytes__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
26 | 51.6k | { |
27 | 51.6k | return bytes___bytes___impl((PyBytesObject *)self); |
28 | 51.6k | } |
29 | | |
30 | | PyDoc_STRVAR(bytes_split__doc__, |
31 | | "split($self, /, sep=None, maxsplit=-1)\n" |
32 | | "--\n" |
33 | | "\n" |
34 | | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
35 | | "\n" |
36 | | " sep\n" |
37 | | " The delimiter according which to split the bytes.\n" |
38 | | " None (the default value) means split on ASCII whitespace characters\n" |
39 | | " (space, tab, return, newline, formfeed, vertical tab).\n" |
40 | | " maxsplit\n" |
41 | | " Maximum number of splits to do.\n" |
42 | | " -1 (the default value) means no limit."); |
43 | | |
44 | | #define BYTES_SPLIT_METHODDEF \ |
45 | | {"split", _PyCFunction_CAST(bytes_split), METH_FASTCALL|METH_KEYWORDS, bytes_split__doc__}, |
46 | | |
47 | | static PyObject * |
48 | | bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit); |
49 | | |
50 | | static PyObject * |
51 | | bytes_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
52 | 2.95M | { |
53 | 2.95M | PyObject *return_value = NULL; |
54 | 2.95M | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
55 | | |
56 | 2.95M | #define NUM_KEYWORDS 2 |
57 | 2.95M | static struct { |
58 | 2.95M | PyGC_Head _this_is_not_used; |
59 | 2.95M | PyObject_VAR_HEAD |
60 | 2.95M | Py_hash_t ob_hash; |
61 | 2.95M | PyObject *ob_item[NUM_KEYWORDS]; |
62 | 2.95M | } _kwtuple = { |
63 | 2.95M | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
64 | 2.95M | .ob_hash = -1, |
65 | 2.95M | .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), }, |
66 | 2.95M | }; |
67 | 2.95M | #undef NUM_KEYWORDS |
68 | 2.95M | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
69 | | |
70 | | #else // !Py_BUILD_CORE |
71 | | # define KWTUPLE NULL |
72 | | #endif // !Py_BUILD_CORE |
73 | | |
74 | 2.95M | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
75 | 2.95M | static _PyArg_Parser _parser = { |
76 | 2.95M | .keywords = _keywords, |
77 | 2.95M | .fname = "split", |
78 | 2.95M | .kwtuple = KWTUPLE, |
79 | 2.95M | }; |
80 | 2.95M | #undef KWTUPLE |
81 | 2.95M | PyObject *argsbuf[2]; |
82 | 2.95M | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
83 | 2.95M | PyObject *sep = Py_None; |
84 | 2.95M | Py_ssize_t maxsplit = -1; |
85 | | |
86 | 2.95M | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
87 | 2.95M | /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
88 | 2.95M | if (!args) { |
89 | 0 | goto exit; |
90 | 0 | } |
91 | 2.95M | if (!noptargs) { |
92 | 0 | goto skip_optional_pos; |
93 | 0 | } |
94 | 2.95M | if (args[0]) { |
95 | 2.95M | sep = args[0]; |
96 | 2.95M | if (!--noptargs) { |
97 | 2.95M | goto skip_optional_pos; |
98 | 2.95M | } |
99 | 2.95M | } |
100 | 0 | { |
101 | 0 | Py_ssize_t ival = -1; |
102 | 0 | PyObject *iobj = _PyNumber_Index(args[1]); |
103 | 0 | if (iobj != NULL) { |
104 | 0 | ival = PyLong_AsSsize_t(iobj); |
105 | 0 | Py_DECREF(iobj); |
106 | 0 | } |
107 | 0 | if (ival == -1 && PyErr_Occurred()) { |
108 | 0 | goto exit; |
109 | 0 | } |
110 | 0 | maxsplit = ival; |
111 | 0 | } |
112 | 2.95M | skip_optional_pos: |
113 | 2.95M | return_value = bytes_split_impl((PyBytesObject *)self, sep, maxsplit); |
114 | | |
115 | 2.95M | exit: |
116 | 2.95M | return return_value; |
117 | 2.95M | } |
118 | | |
119 | | PyDoc_STRVAR(bytes_partition__doc__, |
120 | | "partition($self, sep, /)\n" |
121 | | "--\n" |
122 | | "\n" |
123 | | "Partition the bytes into three parts using the given separator.\n" |
124 | | "\n" |
125 | | "This will search for the separator sep in the bytes. If the separator is found,\n" |
126 | | "returns a 3-tuple containing the part before the separator, the separator\n" |
127 | | "itself, and the part after it.\n" |
128 | | "\n" |
129 | | "If the separator is not found, returns a 3-tuple containing the original bytes\n" |
130 | | "object and two empty bytes objects."); |
131 | | |
132 | | #define BYTES_PARTITION_METHODDEF \ |
133 | | {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__}, |
134 | | |
135 | | static PyObject * |
136 | | bytes_partition_impl(PyBytesObject *self, Py_buffer *sep); |
137 | | |
138 | | static PyObject * |
139 | | bytes_partition(PyObject *self, PyObject *arg) |
140 | 515k | { |
141 | 515k | PyObject *return_value = NULL; |
142 | 515k | Py_buffer sep = {NULL, NULL}; |
143 | | |
144 | 515k | if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) { |
145 | 0 | goto exit; |
146 | 0 | } |
147 | 515k | return_value = bytes_partition_impl((PyBytesObject *)self, &sep); |
148 | | |
149 | 515k | exit: |
150 | | /* Cleanup for sep */ |
151 | 515k | if (sep.obj) { |
152 | 515k | PyBuffer_Release(&sep); |
153 | 515k | } |
154 | | |
155 | 515k | return return_value; |
156 | 515k | } |
157 | | |
158 | | PyDoc_STRVAR(bytes_rpartition__doc__, |
159 | | "rpartition($self, sep, /)\n" |
160 | | "--\n" |
161 | | "\n" |
162 | | "Partition the bytes into three parts using the given separator.\n" |
163 | | "\n" |
164 | | "This will search for the separator sep in the bytes, starting at the end. If\n" |
165 | | "the separator is found, returns a 3-tuple containing the part before the\n" |
166 | | "separator, the separator itself, and the part after it.\n" |
167 | | "\n" |
168 | | "If the separator is not found, returns a 3-tuple containing two empty bytes\n" |
169 | | "objects and the original bytes object."); |
170 | | |
171 | | #define BYTES_RPARTITION_METHODDEF \ |
172 | | {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__}, |
173 | | |
174 | | static PyObject * |
175 | | bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep); |
176 | | |
177 | | static PyObject * |
178 | | bytes_rpartition(PyObject *self, PyObject *arg) |
179 | 0 | { |
180 | 0 | PyObject *return_value = NULL; |
181 | 0 | Py_buffer sep = {NULL, NULL}; |
182 | |
|
183 | 0 | if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) { |
184 | 0 | goto exit; |
185 | 0 | } |
186 | 0 | return_value = bytes_rpartition_impl((PyBytesObject *)self, &sep); |
187 | |
|
188 | 0 | exit: |
189 | | /* Cleanup for sep */ |
190 | 0 | if (sep.obj) { |
191 | 0 | PyBuffer_Release(&sep); |
192 | 0 | } |
193 | |
|
194 | 0 | return return_value; |
195 | 0 | } |
196 | | |
197 | | PyDoc_STRVAR(bytes_rsplit__doc__, |
198 | | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
199 | | "--\n" |
200 | | "\n" |
201 | | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
202 | | "\n" |
203 | | " sep\n" |
204 | | " The delimiter according which to split the bytes.\n" |
205 | | " None (the default value) means split on ASCII whitespace characters\n" |
206 | | " (space, tab, return, newline, formfeed, vertical tab).\n" |
207 | | " maxsplit\n" |
208 | | " Maximum number of splits to do.\n" |
209 | | " -1 (the default value) means no limit.\n" |
210 | | "\n" |
211 | | "Splitting is done starting at the end of the bytes and working to the front."); |
212 | | |
213 | | #define BYTES_RSPLIT_METHODDEF \ |
214 | | {"rsplit", _PyCFunction_CAST(bytes_rsplit), METH_FASTCALL|METH_KEYWORDS, bytes_rsplit__doc__}, |
215 | | |
216 | | static PyObject * |
217 | | bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit); |
218 | | |
219 | | static PyObject * |
220 | | bytes_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
221 | 0 | { |
222 | 0 | PyObject *return_value = NULL; |
223 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
224 | |
|
225 | 0 | #define NUM_KEYWORDS 2 |
226 | 0 | static struct { |
227 | 0 | PyGC_Head _this_is_not_used; |
228 | 0 | PyObject_VAR_HEAD |
229 | 0 | Py_hash_t ob_hash; |
230 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
231 | 0 | } _kwtuple = { |
232 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
233 | 0 | .ob_hash = -1, |
234 | 0 | .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), }, |
235 | 0 | }; |
236 | 0 | #undef NUM_KEYWORDS |
237 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
238 | |
|
239 | | #else // !Py_BUILD_CORE |
240 | | # define KWTUPLE NULL |
241 | | #endif // !Py_BUILD_CORE |
242 | |
|
243 | 0 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
244 | 0 | static _PyArg_Parser _parser = { |
245 | 0 | .keywords = _keywords, |
246 | 0 | .fname = "rsplit", |
247 | 0 | .kwtuple = KWTUPLE, |
248 | 0 | }; |
249 | 0 | #undef KWTUPLE |
250 | 0 | PyObject *argsbuf[2]; |
251 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
252 | 0 | PyObject *sep = Py_None; |
253 | 0 | Py_ssize_t maxsplit = -1; |
254 | |
|
255 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
256 | 0 | /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
257 | 0 | if (!args) { |
258 | 0 | goto exit; |
259 | 0 | } |
260 | 0 | if (!noptargs) { |
261 | 0 | goto skip_optional_pos; |
262 | 0 | } |
263 | 0 | if (args[0]) { |
264 | 0 | sep = args[0]; |
265 | 0 | if (!--noptargs) { |
266 | 0 | goto skip_optional_pos; |
267 | 0 | } |
268 | 0 | } |
269 | 0 | { |
270 | 0 | Py_ssize_t ival = -1; |
271 | 0 | PyObject *iobj = _PyNumber_Index(args[1]); |
272 | 0 | if (iobj != NULL) { |
273 | 0 | ival = PyLong_AsSsize_t(iobj); |
274 | 0 | Py_DECREF(iobj); |
275 | 0 | } |
276 | 0 | if (ival == -1 && PyErr_Occurred()) { |
277 | 0 | goto exit; |
278 | 0 | } |
279 | 0 | maxsplit = ival; |
280 | 0 | } |
281 | 0 | skip_optional_pos: |
282 | 0 | return_value = bytes_rsplit_impl((PyBytesObject *)self, sep, maxsplit); |
283 | |
|
284 | 0 | exit: |
285 | 0 | return return_value; |
286 | 0 | } |
287 | | |
288 | | PyDoc_STRVAR(bytes_join__doc__, |
289 | | "join($self, iterable_of_bytes, /)\n" |
290 | | "--\n" |
291 | | "\n" |
292 | | "Concatenate any number of bytes objects.\n" |
293 | | "\n" |
294 | | "The bytes whose method is called is inserted in between each pair.\n" |
295 | | "\n" |
296 | | "The result is returned as a new bytes object.\n" |
297 | | "\n" |
298 | | "Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'."); |
299 | | |
300 | | #define BYTES_JOIN_METHODDEF \ |
301 | | {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__}, |
302 | | |
303 | | static PyObject * |
304 | | bytes_join_impl(PyBytesObject *self, PyObject *iterable_of_bytes); |
305 | | |
306 | | static PyObject * |
307 | | bytes_join(PyObject *self, PyObject *iterable_of_bytes) |
308 | 271k | { |
309 | 271k | PyObject *return_value = NULL; |
310 | | |
311 | 271k | return_value = bytes_join_impl((PyBytesObject *)self, iterable_of_bytes); |
312 | | |
313 | 271k | return return_value; |
314 | 271k | } |
315 | | |
316 | | PyDoc_STRVAR(bytes_find__doc__, |
317 | | "find($self, sub[, start[, end]], /)\n" |
318 | | "--\n" |
319 | | "\n" |
320 | | "Return the lowest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start,end].\n" |
321 | | "\n" |
322 | | " start\n" |
323 | | " Optional start position. Default: start of the bytes.\n" |
324 | | " end\n" |
325 | | " Optional stop position. Default: end of the bytes.\n" |
326 | | "\n" |
327 | | "Return -1 on failure."); |
328 | | |
329 | | #define BYTES_FIND_METHODDEF \ |
330 | | {"find", _PyCFunction_CAST(bytes_find), METH_FASTCALL, bytes_find__doc__}, |
331 | | |
332 | | static PyObject * |
333 | | bytes_find_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start, |
334 | | Py_ssize_t end); |
335 | | |
336 | | static PyObject * |
337 | | bytes_find(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
338 | 16.8M | { |
339 | 16.8M | PyObject *return_value = NULL; |
340 | 16.8M | PyObject *sub; |
341 | 16.8M | Py_ssize_t start = 0; |
342 | 16.8M | Py_ssize_t end = PY_SSIZE_T_MAX; |
343 | | |
344 | 16.8M | if (!_PyArg_CheckPositional("find", nargs, 1, 3)) { |
345 | 0 | goto exit; |
346 | 0 | } |
347 | 16.8M | sub = args[0]; |
348 | 16.8M | if (nargs < 2) { |
349 | 16.8M | goto skip_optional; |
350 | 16.8M | } |
351 | 32.1k | if (!_PyEval_SliceIndex(args[1], &start)) { |
352 | 0 | goto exit; |
353 | 0 | } |
354 | 32.1k | if (nargs < 3) { |
355 | 32.1k | goto skip_optional; |
356 | 32.1k | } |
357 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
358 | 0 | goto exit; |
359 | 0 | } |
360 | 16.8M | skip_optional: |
361 | 16.8M | return_value = bytes_find_impl((PyBytesObject *)self, sub, start, end); |
362 | | |
363 | 16.8M | exit: |
364 | 16.8M | return return_value; |
365 | 16.8M | } |
366 | | |
367 | | PyDoc_STRVAR(bytes_index__doc__, |
368 | | "index($self, sub[, start[, end]], /)\n" |
369 | | "--\n" |
370 | | "\n" |
371 | | "Return the lowest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start,end].\n" |
372 | | "\n" |
373 | | " start\n" |
374 | | " Optional start position. Default: start of the bytes.\n" |
375 | | " end\n" |
376 | | " Optional stop position. Default: end of the bytes.\n" |
377 | | "\n" |
378 | | "Raise ValueError if the subsection is not found."); |
379 | | |
380 | | #define BYTES_INDEX_METHODDEF \ |
381 | | {"index", _PyCFunction_CAST(bytes_index), METH_FASTCALL, bytes_index__doc__}, |
382 | | |
383 | | static PyObject * |
384 | | bytes_index_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start, |
385 | | Py_ssize_t end); |
386 | | |
387 | | static PyObject * |
388 | | bytes_index(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
389 | 0 | { |
390 | 0 | PyObject *return_value = NULL; |
391 | 0 | PyObject *sub; |
392 | 0 | Py_ssize_t start = 0; |
393 | 0 | Py_ssize_t end = PY_SSIZE_T_MAX; |
394 | |
|
395 | 0 | if (!_PyArg_CheckPositional("index", nargs, 1, 3)) { |
396 | 0 | goto exit; |
397 | 0 | } |
398 | 0 | sub = args[0]; |
399 | 0 | if (nargs < 2) { |
400 | 0 | goto skip_optional; |
401 | 0 | } |
402 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
403 | 0 | goto exit; |
404 | 0 | } |
405 | 0 | if (nargs < 3) { |
406 | 0 | goto skip_optional; |
407 | 0 | } |
408 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
409 | 0 | goto exit; |
410 | 0 | } |
411 | 0 | skip_optional: |
412 | 0 | return_value = bytes_index_impl((PyBytesObject *)self, sub, start, end); |
413 | |
|
414 | 0 | exit: |
415 | 0 | return return_value; |
416 | 0 | } |
417 | | |
418 | | PyDoc_STRVAR(bytes_rfind__doc__, |
419 | | "rfind($self, sub[, start[, end]], /)\n" |
420 | | "--\n" |
421 | | "\n" |
422 | | "Return the highest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start,end].\n" |
423 | | "\n" |
424 | | " start\n" |
425 | | " Optional start position. Default: start of the bytes.\n" |
426 | | " end\n" |
427 | | " Optional stop position. Default: end of the bytes.\n" |
428 | | "\n" |
429 | | "Return -1 on failure."); |
430 | | |
431 | | #define BYTES_RFIND_METHODDEF \ |
432 | | {"rfind", _PyCFunction_CAST(bytes_rfind), METH_FASTCALL, bytes_rfind__doc__}, |
433 | | |
434 | | static PyObject * |
435 | | bytes_rfind_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start, |
436 | | Py_ssize_t end); |
437 | | |
438 | | static PyObject * |
439 | | bytes_rfind(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
440 | 14.2k | { |
441 | 14.2k | PyObject *return_value = NULL; |
442 | 14.2k | PyObject *sub; |
443 | 14.2k | Py_ssize_t start = 0; |
444 | 14.2k | Py_ssize_t end = PY_SSIZE_T_MAX; |
445 | | |
446 | 14.2k | if (!_PyArg_CheckPositional("rfind", nargs, 1, 3)) { |
447 | 0 | goto exit; |
448 | 0 | } |
449 | 14.2k | sub = args[0]; |
450 | 14.2k | if (nargs < 2) { |
451 | 14.2k | goto skip_optional; |
452 | 14.2k | } |
453 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
454 | 0 | goto exit; |
455 | 0 | } |
456 | 0 | if (nargs < 3) { |
457 | 0 | goto skip_optional; |
458 | 0 | } |
459 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
460 | 0 | goto exit; |
461 | 0 | } |
462 | 14.2k | skip_optional: |
463 | 14.2k | return_value = bytes_rfind_impl((PyBytesObject *)self, sub, start, end); |
464 | | |
465 | 14.2k | exit: |
466 | 14.2k | return return_value; |
467 | 14.2k | } |
468 | | |
469 | | PyDoc_STRVAR(bytes_rindex__doc__, |
470 | | "rindex($self, sub[, start[, end]], /)\n" |
471 | | "--\n" |
472 | | "\n" |
473 | | "Return the highest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start,end].\n" |
474 | | "\n" |
475 | | " start\n" |
476 | | " Optional start position. Default: start of the bytes.\n" |
477 | | " end\n" |
478 | | " Optional stop position. Default: end of the bytes.\n" |
479 | | "\n" |
480 | | "Raise ValueError if the subsection is not found."); |
481 | | |
482 | | #define BYTES_RINDEX_METHODDEF \ |
483 | | {"rindex", _PyCFunction_CAST(bytes_rindex), METH_FASTCALL, bytes_rindex__doc__}, |
484 | | |
485 | | static PyObject * |
486 | | bytes_rindex_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start, |
487 | | Py_ssize_t end); |
488 | | |
489 | | static PyObject * |
490 | | bytes_rindex(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
491 | 0 | { |
492 | 0 | PyObject *return_value = NULL; |
493 | 0 | PyObject *sub; |
494 | 0 | Py_ssize_t start = 0; |
495 | 0 | Py_ssize_t end = PY_SSIZE_T_MAX; |
496 | |
|
497 | 0 | if (!_PyArg_CheckPositional("rindex", nargs, 1, 3)) { |
498 | 0 | goto exit; |
499 | 0 | } |
500 | 0 | sub = args[0]; |
501 | 0 | if (nargs < 2) { |
502 | 0 | goto skip_optional; |
503 | 0 | } |
504 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
505 | 0 | goto exit; |
506 | 0 | } |
507 | 0 | if (nargs < 3) { |
508 | 0 | goto skip_optional; |
509 | 0 | } |
510 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
511 | 0 | goto exit; |
512 | 0 | } |
513 | 0 | skip_optional: |
514 | 0 | return_value = bytes_rindex_impl((PyBytesObject *)self, sub, start, end); |
515 | |
|
516 | 0 | exit: |
517 | 0 | return return_value; |
518 | 0 | } |
519 | | |
520 | | PyDoc_STRVAR(bytes_strip__doc__, |
521 | | "strip($self, bytes=None, /)\n" |
522 | | "--\n" |
523 | | "\n" |
524 | | "Strip leading and trailing bytes contained in the argument.\n" |
525 | | "\n" |
526 | | "If the argument is omitted or None, strip leading and trailing ASCII whitespace."); |
527 | | |
528 | | #define BYTES_STRIP_METHODDEF \ |
529 | | {"strip", _PyCFunction_CAST(bytes_strip), METH_FASTCALL, bytes_strip__doc__}, |
530 | | |
531 | | static PyObject * |
532 | | bytes_strip_impl(PyBytesObject *self, PyObject *bytes); |
533 | | |
534 | | static PyObject * |
535 | | bytes_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
536 | 0 | { |
537 | 0 | PyObject *return_value = NULL; |
538 | 0 | PyObject *bytes = Py_None; |
539 | |
|
540 | 0 | if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) { |
541 | 0 | goto exit; |
542 | 0 | } |
543 | 0 | if (nargs < 1) { |
544 | 0 | goto skip_optional; |
545 | 0 | } |
546 | 0 | bytes = args[0]; |
547 | 0 | skip_optional: |
548 | 0 | return_value = bytes_strip_impl((PyBytesObject *)self, bytes); |
549 | |
|
550 | 0 | exit: |
551 | 0 | return return_value; |
552 | 0 | } |
553 | | |
554 | | PyDoc_STRVAR(bytes_lstrip__doc__, |
555 | | "lstrip($self, bytes=None, /)\n" |
556 | | "--\n" |
557 | | "\n" |
558 | | "Strip leading bytes contained in the argument.\n" |
559 | | "\n" |
560 | | "If the argument is omitted or None, strip leading ASCII whitespace."); |
561 | | |
562 | | #define BYTES_LSTRIP_METHODDEF \ |
563 | | {"lstrip", _PyCFunction_CAST(bytes_lstrip), METH_FASTCALL, bytes_lstrip__doc__}, |
564 | | |
565 | | static PyObject * |
566 | | bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes); |
567 | | |
568 | | static PyObject * |
569 | | bytes_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
570 | 0 | { |
571 | 0 | PyObject *return_value = NULL; |
572 | 0 | PyObject *bytes = Py_None; |
573 | |
|
574 | 0 | if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) { |
575 | 0 | goto exit; |
576 | 0 | } |
577 | 0 | if (nargs < 1) { |
578 | 0 | goto skip_optional; |
579 | 0 | } |
580 | 0 | bytes = args[0]; |
581 | 0 | skip_optional: |
582 | 0 | return_value = bytes_lstrip_impl((PyBytesObject *)self, bytes); |
583 | |
|
584 | 0 | exit: |
585 | 0 | return return_value; |
586 | 0 | } |
587 | | |
588 | | PyDoc_STRVAR(bytes_rstrip__doc__, |
589 | | "rstrip($self, bytes=None, /)\n" |
590 | | "--\n" |
591 | | "\n" |
592 | | "Strip trailing bytes contained in the argument.\n" |
593 | | "\n" |
594 | | "If the argument is omitted or None, strip trailing ASCII whitespace."); |
595 | | |
596 | | #define BYTES_RSTRIP_METHODDEF \ |
597 | | {"rstrip", _PyCFunction_CAST(bytes_rstrip), METH_FASTCALL, bytes_rstrip__doc__}, |
598 | | |
599 | | static PyObject * |
600 | | bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes); |
601 | | |
602 | | static PyObject * |
603 | | bytes_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
604 | 294 | { |
605 | 294 | PyObject *return_value = NULL; |
606 | 294 | PyObject *bytes = Py_None; |
607 | | |
608 | 294 | if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) { |
609 | 0 | goto exit; |
610 | 0 | } |
611 | 294 | if (nargs < 1) { |
612 | 0 | goto skip_optional; |
613 | 0 | } |
614 | 294 | bytes = args[0]; |
615 | 294 | skip_optional: |
616 | 294 | return_value = bytes_rstrip_impl((PyBytesObject *)self, bytes); |
617 | | |
618 | 294 | exit: |
619 | 294 | return return_value; |
620 | 294 | } |
621 | | |
622 | | PyDoc_STRVAR(bytes_count__doc__, |
623 | | "count($self, sub[, start[, end]], /)\n" |
624 | | "--\n" |
625 | | "\n" |
626 | | "Return the number of non-overlapping occurrences of subsection \'sub\' in bytes B[start:end].\n" |
627 | | "\n" |
628 | | " start\n" |
629 | | " Optional start position. Default: start of the bytes.\n" |
630 | | " end\n" |
631 | | " Optional stop position. Default: end of the bytes."); |
632 | | |
633 | | #define BYTES_COUNT_METHODDEF \ |
634 | | {"count", _PyCFunction_CAST(bytes_count), METH_FASTCALL, bytes_count__doc__}, |
635 | | |
636 | | static PyObject * |
637 | | bytes_count_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start, |
638 | | Py_ssize_t end); |
639 | | |
640 | | static PyObject * |
641 | | bytes_count(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
642 | 6.52M | { |
643 | 6.52M | PyObject *return_value = NULL; |
644 | 6.52M | PyObject *sub; |
645 | 6.52M | Py_ssize_t start = 0; |
646 | 6.52M | Py_ssize_t end = PY_SSIZE_T_MAX; |
647 | | |
648 | 6.52M | if (!_PyArg_CheckPositional("count", nargs, 1, 3)) { |
649 | 0 | goto exit; |
650 | 0 | } |
651 | 6.52M | sub = args[0]; |
652 | 6.52M | if (nargs < 2) { |
653 | 6.52M | goto skip_optional; |
654 | 6.52M | } |
655 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
656 | 0 | goto exit; |
657 | 0 | } |
658 | 0 | if (nargs < 3) { |
659 | 0 | goto skip_optional; |
660 | 0 | } |
661 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
662 | 0 | goto exit; |
663 | 0 | } |
664 | 6.52M | skip_optional: |
665 | 6.52M | return_value = bytes_count_impl((PyBytesObject *)self, sub, start, end); |
666 | | |
667 | 6.52M | exit: |
668 | 6.52M | return return_value; |
669 | 6.52M | } |
670 | | |
671 | | PyDoc_STRVAR(bytes_translate__doc__, |
672 | | "translate($self, table, /, delete=b\'\')\n" |
673 | | "--\n" |
674 | | "\n" |
675 | | "Return a copy with each character mapped by the given translation table.\n" |
676 | | "\n" |
677 | | " table\n" |
678 | | " Translation table, which must be a bytes object of length 256.\n" |
679 | | "\n" |
680 | | "All characters occurring in the optional argument delete are removed.\n" |
681 | | "The remaining characters are mapped through the given translation table."); |
682 | | |
683 | | #define BYTES_TRANSLATE_METHODDEF \ |
684 | | {"translate", _PyCFunction_CAST(bytes_translate), METH_FASTCALL|METH_KEYWORDS, bytes_translate__doc__}, |
685 | | |
686 | | static PyObject * |
687 | | bytes_translate_impl(PyBytesObject *self, PyObject *table, |
688 | | PyObject *deletechars); |
689 | | |
690 | | static PyObject * |
691 | | bytes_translate(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
692 | 0 | { |
693 | 0 | PyObject *return_value = NULL; |
694 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
695 | |
|
696 | 0 | #define NUM_KEYWORDS 1 |
697 | 0 | static struct { |
698 | 0 | PyGC_Head _this_is_not_used; |
699 | 0 | PyObject_VAR_HEAD |
700 | 0 | Py_hash_t ob_hash; |
701 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
702 | 0 | } _kwtuple = { |
703 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
704 | 0 | .ob_hash = -1, |
705 | 0 | .ob_item = { &_Py_ID(delete), }, |
706 | 0 | }; |
707 | 0 | #undef NUM_KEYWORDS |
708 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
709 | |
|
710 | | #else // !Py_BUILD_CORE |
711 | | # define KWTUPLE NULL |
712 | | #endif // !Py_BUILD_CORE |
713 | |
|
714 | 0 | static const char * const _keywords[] = {"", "delete", NULL}; |
715 | 0 | static _PyArg_Parser _parser = { |
716 | 0 | .keywords = _keywords, |
717 | 0 | .fname = "translate", |
718 | 0 | .kwtuple = KWTUPLE, |
719 | 0 | }; |
720 | 0 | #undef KWTUPLE |
721 | 0 | PyObject *argsbuf[2]; |
722 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
723 | 0 | PyObject *table; |
724 | 0 | PyObject *deletechars = NULL; |
725 | |
|
726 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
727 | 0 | /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
728 | 0 | if (!args) { |
729 | 0 | goto exit; |
730 | 0 | } |
731 | 0 | table = args[0]; |
732 | 0 | if (!noptargs) { |
733 | 0 | goto skip_optional_pos; |
734 | 0 | } |
735 | 0 | deletechars = args[1]; |
736 | 0 | skip_optional_pos: |
737 | 0 | return_value = bytes_translate_impl((PyBytesObject *)self, table, deletechars); |
738 | |
|
739 | 0 | exit: |
740 | 0 | return return_value; |
741 | 0 | } |
742 | | |
743 | | PyDoc_STRVAR(bytes_maketrans__doc__, |
744 | | "maketrans(frm, to, /)\n" |
745 | | "--\n" |
746 | | "\n" |
747 | | "Return a translation table usable for the bytes or bytearray translate method.\n" |
748 | | "\n" |
749 | | "The returned table will be one where each byte in frm is mapped to the byte at\n" |
750 | | "the same position in to.\n" |
751 | | "\n" |
752 | | "The bytes objects frm and to must be of the same length."); |
753 | | |
754 | | #define BYTES_MAKETRANS_METHODDEF \ |
755 | | {"maketrans", _PyCFunction_CAST(bytes_maketrans), METH_FASTCALL|METH_STATIC, bytes_maketrans__doc__}, |
756 | | |
757 | | static PyObject * |
758 | | bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to); |
759 | | |
760 | | static PyObject * |
761 | | bytes_maketrans(PyObject *null, PyObject *const *args, Py_ssize_t nargs) |
762 | 9 | { |
763 | 9 | PyObject *return_value = NULL; |
764 | 9 | Py_buffer frm = {NULL, NULL}; |
765 | 9 | Py_buffer to = {NULL, NULL}; |
766 | | |
767 | 9 | if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) { |
768 | 0 | goto exit; |
769 | 0 | } |
770 | 9 | if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) { |
771 | 0 | goto exit; |
772 | 0 | } |
773 | 9 | if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) { |
774 | 0 | goto exit; |
775 | 0 | } |
776 | 9 | return_value = bytes_maketrans_impl(&frm, &to); |
777 | | |
778 | 9 | exit: |
779 | | /* Cleanup for frm */ |
780 | 9 | if (frm.obj) { |
781 | 9 | PyBuffer_Release(&frm); |
782 | 9 | } |
783 | | /* Cleanup for to */ |
784 | 9 | if (to.obj) { |
785 | 9 | PyBuffer_Release(&to); |
786 | 9 | } |
787 | | |
788 | 9 | return return_value; |
789 | 9 | } |
790 | | |
791 | | PyDoc_STRVAR(bytes_replace__doc__, |
792 | | "replace($self, old, new, /, count=-1)\n" |
793 | | "--\n" |
794 | | "\n" |
795 | | "Return a copy with all occurrences of substring old replaced by new.\n" |
796 | | "\n" |
797 | | " count\n" |
798 | | " Maximum number of occurrences to replace.\n" |
799 | | " -1 (the default value) means replace all occurrences.\n" |
800 | | "\n" |
801 | | "If count is given, only the first count occurrences are replaced.\n" |
802 | | "If count is not specified or -1, then all occurrences are replaced."); |
803 | | |
804 | | #define BYTES_REPLACE_METHODDEF \ |
805 | | {"replace", _PyCFunction_CAST(bytes_replace), METH_FASTCALL|METH_KEYWORDS, bytes_replace__doc__}, |
806 | | |
807 | | static PyObject * |
808 | | bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new, |
809 | | Py_ssize_t count); |
810 | | |
811 | | static PyObject * |
812 | | bytes_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
813 | 32.5k | { |
814 | 32.5k | PyObject *return_value = NULL; |
815 | 32.5k | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
816 | | |
817 | 32.5k | #define NUM_KEYWORDS 1 |
818 | 32.5k | static struct { |
819 | 32.5k | PyGC_Head _this_is_not_used; |
820 | 32.5k | PyObject_VAR_HEAD |
821 | 32.5k | Py_hash_t ob_hash; |
822 | 32.5k | PyObject *ob_item[NUM_KEYWORDS]; |
823 | 32.5k | } _kwtuple = { |
824 | 32.5k | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
825 | 32.5k | .ob_hash = -1, |
826 | 32.5k | .ob_item = { &_Py_ID(count), }, |
827 | 32.5k | }; |
828 | 32.5k | #undef NUM_KEYWORDS |
829 | 32.5k | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
830 | | |
831 | | #else // !Py_BUILD_CORE |
832 | | # define KWTUPLE NULL |
833 | | #endif // !Py_BUILD_CORE |
834 | | |
835 | 32.5k | static const char * const _keywords[] = {"", "", "count", NULL}; |
836 | 32.5k | static _PyArg_Parser _parser = { |
837 | 32.5k | .keywords = _keywords, |
838 | 32.5k | .fname = "replace", |
839 | 32.5k | .kwtuple = KWTUPLE, |
840 | 32.5k | }; |
841 | 32.5k | #undef KWTUPLE |
842 | 32.5k | PyObject *argsbuf[3]; |
843 | 32.5k | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
844 | 32.5k | Py_buffer old = {NULL, NULL}; |
845 | 32.5k | Py_buffer new = {NULL, NULL}; |
846 | 32.5k | Py_ssize_t count = -1; |
847 | | |
848 | 32.5k | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
849 | 32.5k | /*minpos*/ 2, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
850 | 32.5k | if (!args) { |
851 | 0 | goto exit; |
852 | 0 | } |
853 | 32.5k | if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) { |
854 | 0 | goto exit; |
855 | 0 | } |
856 | 32.5k | if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) { |
857 | 0 | goto exit; |
858 | 0 | } |
859 | 32.5k | if (!noptargs) { |
860 | 32.5k | goto skip_optional_pos; |
861 | 32.5k | } |
862 | 0 | { |
863 | 0 | Py_ssize_t ival = -1; |
864 | 0 | PyObject *iobj = _PyNumber_Index(args[2]); |
865 | 0 | if (iobj != NULL) { |
866 | 0 | ival = PyLong_AsSsize_t(iobj); |
867 | 0 | Py_DECREF(iobj); |
868 | 0 | } |
869 | 0 | if (ival == -1 && PyErr_Occurred()) { |
870 | 0 | goto exit; |
871 | 0 | } |
872 | 0 | count = ival; |
873 | 0 | } |
874 | 32.5k | skip_optional_pos: |
875 | 32.5k | return_value = bytes_replace_impl((PyBytesObject *)self, &old, &new, count); |
876 | | |
877 | 32.5k | exit: |
878 | | /* Cleanup for old */ |
879 | 32.5k | if (old.obj) { |
880 | 32.5k | PyBuffer_Release(&old); |
881 | 32.5k | } |
882 | | /* Cleanup for new */ |
883 | 32.5k | if (new.obj) { |
884 | 32.5k | PyBuffer_Release(&new); |
885 | 32.5k | } |
886 | | |
887 | 32.5k | return return_value; |
888 | 32.5k | } |
889 | | |
890 | | PyDoc_STRVAR(bytes_removeprefix__doc__, |
891 | | "removeprefix($self, prefix, /)\n" |
892 | | "--\n" |
893 | | "\n" |
894 | | "Return a bytes object with the given prefix string removed if present.\n" |
895 | | "\n" |
896 | | "If the bytes starts with the prefix string, return bytes[len(prefix):].\n" |
897 | | "Otherwise, return a copy of the original bytes."); |
898 | | |
899 | | #define BYTES_REMOVEPREFIX_METHODDEF \ |
900 | | {"removeprefix", (PyCFunction)bytes_removeprefix, METH_O, bytes_removeprefix__doc__}, |
901 | | |
902 | | static PyObject * |
903 | | bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix); |
904 | | |
905 | | static PyObject * |
906 | | bytes_removeprefix(PyObject *self, PyObject *arg) |
907 | 0 | { |
908 | 0 | PyObject *return_value = NULL; |
909 | 0 | Py_buffer prefix = {NULL, NULL}; |
910 | |
|
911 | 0 | if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) { |
912 | 0 | goto exit; |
913 | 0 | } |
914 | 0 | return_value = bytes_removeprefix_impl((PyBytesObject *)self, &prefix); |
915 | |
|
916 | 0 | exit: |
917 | | /* Cleanup for prefix */ |
918 | 0 | if (prefix.obj) { |
919 | 0 | PyBuffer_Release(&prefix); |
920 | 0 | } |
921 | |
|
922 | 0 | return return_value; |
923 | 0 | } |
924 | | |
925 | | PyDoc_STRVAR(bytes_removesuffix__doc__, |
926 | | "removesuffix($self, suffix, /)\n" |
927 | | "--\n" |
928 | | "\n" |
929 | | "Return a bytes object with the given suffix string removed if present.\n" |
930 | | "\n" |
931 | | "If the bytes ends with the suffix string and that suffix is not empty,\n" |
932 | | "return bytes[:-len(prefix)]. Otherwise, return a copy of the original\n" |
933 | | "bytes."); |
934 | | |
935 | | #define BYTES_REMOVESUFFIX_METHODDEF \ |
936 | | {"removesuffix", (PyCFunction)bytes_removesuffix, METH_O, bytes_removesuffix__doc__}, |
937 | | |
938 | | static PyObject * |
939 | | bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix); |
940 | | |
941 | | static PyObject * |
942 | | bytes_removesuffix(PyObject *self, PyObject *arg) |
943 | 0 | { |
944 | 0 | PyObject *return_value = NULL; |
945 | 0 | Py_buffer suffix = {NULL, NULL}; |
946 | |
|
947 | 0 | if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) { |
948 | 0 | goto exit; |
949 | 0 | } |
950 | 0 | return_value = bytes_removesuffix_impl((PyBytesObject *)self, &suffix); |
951 | |
|
952 | 0 | exit: |
953 | | /* Cleanup for suffix */ |
954 | 0 | if (suffix.obj) { |
955 | 0 | PyBuffer_Release(&suffix); |
956 | 0 | } |
957 | |
|
958 | 0 | return return_value; |
959 | 0 | } |
960 | | |
961 | | PyDoc_STRVAR(bytes_startswith__doc__, |
962 | | "startswith($self, prefix[, start[, end]], /)\n" |
963 | | "--\n" |
964 | | "\n" |
965 | | "Return True if the bytes starts with the specified prefix, False otherwise.\n" |
966 | | "\n" |
967 | | " prefix\n" |
968 | | " A bytes or a tuple of bytes to try.\n" |
969 | | " start\n" |
970 | | " Optional start position. Default: start of the bytes.\n" |
971 | | " end\n" |
972 | | " Optional stop position. Default: end of the bytes."); |
973 | | |
974 | | #define BYTES_STARTSWITH_METHODDEF \ |
975 | | {"startswith", _PyCFunction_CAST(bytes_startswith), METH_FASTCALL, bytes_startswith__doc__}, |
976 | | |
977 | | static PyObject * |
978 | | bytes_startswith_impl(PyBytesObject *self, PyObject *subobj, |
979 | | Py_ssize_t start, Py_ssize_t end); |
980 | | |
981 | | static PyObject * |
982 | | bytes_startswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
983 | 43.6k | { |
984 | 43.6k | PyObject *return_value = NULL; |
985 | 43.6k | PyObject *subobj; |
986 | 43.6k | Py_ssize_t start = 0; |
987 | 43.6k | Py_ssize_t end = PY_SSIZE_T_MAX; |
988 | | |
989 | 43.6k | if (!_PyArg_CheckPositional("startswith", nargs, 1, 3)) { |
990 | 0 | goto exit; |
991 | 0 | } |
992 | 43.6k | subobj = args[0]; |
993 | 43.6k | if (nargs < 2) { |
994 | 43.6k | goto skip_optional; |
995 | 43.6k | } |
996 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
997 | 0 | goto exit; |
998 | 0 | } |
999 | 0 | if (nargs < 3) { |
1000 | 0 | goto skip_optional; |
1001 | 0 | } |
1002 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
1003 | 0 | goto exit; |
1004 | 0 | } |
1005 | 43.6k | skip_optional: |
1006 | 43.6k | return_value = bytes_startswith_impl((PyBytesObject *)self, subobj, start, end); |
1007 | | |
1008 | 43.6k | exit: |
1009 | 43.6k | return return_value; |
1010 | 43.6k | } |
1011 | | |
1012 | | PyDoc_STRVAR(bytes_endswith__doc__, |
1013 | | "endswith($self, suffix[, start[, end]], /)\n" |
1014 | | "--\n" |
1015 | | "\n" |
1016 | | "Return True if the bytes ends with the specified suffix, False otherwise.\n" |
1017 | | "\n" |
1018 | | " suffix\n" |
1019 | | " A bytes or a tuple of bytes to try.\n" |
1020 | | " start\n" |
1021 | | " Optional start position. Default: start of the bytes.\n" |
1022 | | " end\n" |
1023 | | " Optional stop position. Default: end of the bytes."); |
1024 | | |
1025 | | #define BYTES_ENDSWITH_METHODDEF \ |
1026 | | {"endswith", _PyCFunction_CAST(bytes_endswith), METH_FASTCALL, bytes_endswith__doc__}, |
1027 | | |
1028 | | static PyObject * |
1029 | | bytes_endswith_impl(PyBytesObject *self, PyObject *subobj, Py_ssize_t start, |
1030 | | Py_ssize_t end); |
1031 | | |
1032 | | static PyObject * |
1033 | | bytes_endswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
1034 | 315 | { |
1035 | 315 | PyObject *return_value = NULL; |
1036 | 315 | PyObject *subobj; |
1037 | 315 | Py_ssize_t start = 0; |
1038 | 315 | Py_ssize_t end = PY_SSIZE_T_MAX; |
1039 | | |
1040 | 315 | if (!_PyArg_CheckPositional("endswith", nargs, 1, 3)) { |
1041 | 0 | goto exit; |
1042 | 0 | } |
1043 | 315 | subobj = args[0]; |
1044 | 315 | if (nargs < 2) { |
1045 | 315 | goto skip_optional; |
1046 | 315 | } |
1047 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
1048 | 0 | goto exit; |
1049 | 0 | } |
1050 | 0 | if (nargs < 3) { |
1051 | 0 | goto skip_optional; |
1052 | 0 | } |
1053 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
1054 | 0 | goto exit; |
1055 | 0 | } |
1056 | 315 | skip_optional: |
1057 | 315 | return_value = bytes_endswith_impl((PyBytesObject *)self, subobj, start, end); |
1058 | | |
1059 | 315 | exit: |
1060 | 315 | return return_value; |
1061 | 315 | } |
1062 | | |
1063 | | PyDoc_STRVAR(bytes_decode__doc__, |
1064 | | "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
1065 | | "--\n" |
1066 | | "\n" |
1067 | | "Decode the bytes using the codec registered for encoding.\n" |
1068 | | "\n" |
1069 | | " encoding\n" |
1070 | | " The encoding with which to decode the bytes.\n" |
1071 | | " errors\n" |
1072 | | " The error handling scheme to use for the handling of decoding errors.\n" |
1073 | | " The default is \'strict\' meaning that decoding errors raise a\n" |
1074 | | " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n" |
1075 | | " as well as any other name registered with codecs.register_error that\n" |
1076 | | " can handle UnicodeDecodeErrors."); |
1077 | | |
1078 | | #define BYTES_DECODE_METHODDEF \ |
1079 | | {"decode", _PyCFunction_CAST(bytes_decode), METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__}, |
1080 | | |
1081 | | static PyObject * |
1082 | | bytes_decode_impl(PyBytesObject *self, const char *encoding, |
1083 | | const char *errors); |
1084 | | |
1085 | | static PyObject * |
1086 | | bytes_decode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
1087 | 22.4M | { |
1088 | 22.4M | PyObject *return_value = NULL; |
1089 | 22.4M | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
1090 | | |
1091 | 22.4M | #define NUM_KEYWORDS 2 |
1092 | 22.4M | static struct { |
1093 | 22.4M | PyGC_Head _this_is_not_used; |
1094 | 22.4M | PyObject_VAR_HEAD |
1095 | 22.4M | Py_hash_t ob_hash; |
1096 | 22.4M | PyObject *ob_item[NUM_KEYWORDS]; |
1097 | 22.4M | } _kwtuple = { |
1098 | 22.4M | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
1099 | 22.4M | .ob_hash = -1, |
1100 | 22.4M | .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), }, |
1101 | 22.4M | }; |
1102 | 22.4M | #undef NUM_KEYWORDS |
1103 | 22.4M | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
1104 | | |
1105 | | #else // !Py_BUILD_CORE |
1106 | | # define KWTUPLE NULL |
1107 | | #endif // !Py_BUILD_CORE |
1108 | | |
1109 | 22.4M | static const char * const _keywords[] = {"encoding", "errors", NULL}; |
1110 | 22.4M | static _PyArg_Parser _parser = { |
1111 | 22.4M | .keywords = _keywords, |
1112 | 22.4M | .fname = "decode", |
1113 | 22.4M | .kwtuple = KWTUPLE, |
1114 | 22.4M | }; |
1115 | 22.4M | #undef KWTUPLE |
1116 | 22.4M | PyObject *argsbuf[2]; |
1117 | 22.4M | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
1118 | 22.4M | const char *encoding = NULL; |
1119 | 22.4M | const char *errors = NULL; |
1120 | | |
1121 | 22.4M | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
1122 | 22.4M | /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
1123 | 22.4M | if (!args) { |
1124 | 0 | goto exit; |
1125 | 0 | } |
1126 | 22.4M | if (!noptargs) { |
1127 | 63.0k | goto skip_optional_pos; |
1128 | 63.0k | } |
1129 | 22.3M | if (args[0]) { |
1130 | 22.3M | if (!PyUnicode_Check(args[0])) { |
1131 | 0 | _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]); |
1132 | 0 | goto exit; |
1133 | 0 | } |
1134 | 22.3M | Py_ssize_t encoding_length; |
1135 | 22.3M | encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length); |
1136 | 22.3M | if (encoding == NULL) { |
1137 | 0 | goto exit; |
1138 | 0 | } |
1139 | 22.3M | if (strlen(encoding) != (size_t)encoding_length) { |
1140 | 1.15k | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
1141 | 1.15k | goto exit; |
1142 | 1.15k | } |
1143 | 22.3M | if (!--noptargs) { |
1144 | 1.22M | goto skip_optional_pos; |
1145 | 1.22M | } |
1146 | 22.3M | } |
1147 | 21.1M | if (!PyUnicode_Check(args[1])) { |
1148 | 0 | _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]); |
1149 | 0 | goto exit; |
1150 | 0 | } |
1151 | 21.1M | Py_ssize_t errors_length; |
1152 | 21.1M | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
1153 | 21.1M | if (errors == NULL) { |
1154 | 0 | goto exit; |
1155 | 0 | } |
1156 | 21.1M | if (strlen(errors) != (size_t)errors_length) { |
1157 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
1158 | 0 | goto exit; |
1159 | 0 | } |
1160 | 22.4M | skip_optional_pos: |
1161 | 22.4M | return_value = bytes_decode_impl((PyBytesObject *)self, encoding, errors); |
1162 | | |
1163 | 22.4M | exit: |
1164 | 22.4M | return return_value; |
1165 | 22.4M | } |
1166 | | |
1167 | | PyDoc_STRVAR(bytes_splitlines__doc__, |
1168 | | "splitlines($self, /, keepends=False)\n" |
1169 | | "--\n" |
1170 | | "\n" |
1171 | | "Return a list of the lines in the bytes, breaking at line boundaries.\n" |
1172 | | "\n" |
1173 | | "Line breaks are not included in the resulting list unless keepends is given and\n" |
1174 | | "true."); |
1175 | | |
1176 | | #define BYTES_SPLITLINES_METHODDEF \ |
1177 | | {"splitlines", _PyCFunction_CAST(bytes_splitlines), METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__}, |
1178 | | |
1179 | | static PyObject * |
1180 | | bytes_splitlines_impl(PyBytesObject *self, int keepends); |
1181 | | |
1182 | | static PyObject * |
1183 | | bytes_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
1184 | 0 | { |
1185 | 0 | PyObject *return_value = NULL; |
1186 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
1187 | |
|
1188 | 0 | #define NUM_KEYWORDS 1 |
1189 | 0 | static struct { |
1190 | 0 | PyGC_Head _this_is_not_used; |
1191 | 0 | PyObject_VAR_HEAD |
1192 | 0 | Py_hash_t ob_hash; |
1193 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
1194 | 0 | } _kwtuple = { |
1195 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
1196 | 0 | .ob_hash = -1, |
1197 | 0 | .ob_item = { &_Py_ID(keepends), }, |
1198 | 0 | }; |
1199 | 0 | #undef NUM_KEYWORDS |
1200 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
1201 | |
|
1202 | | #else // !Py_BUILD_CORE |
1203 | | # define KWTUPLE NULL |
1204 | | #endif // !Py_BUILD_CORE |
1205 | |
|
1206 | 0 | static const char * const _keywords[] = {"keepends", NULL}; |
1207 | 0 | static _PyArg_Parser _parser = { |
1208 | 0 | .keywords = _keywords, |
1209 | 0 | .fname = "splitlines", |
1210 | 0 | .kwtuple = KWTUPLE, |
1211 | 0 | }; |
1212 | 0 | #undef KWTUPLE |
1213 | 0 | PyObject *argsbuf[1]; |
1214 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
1215 | 0 | int keepends = 0; |
1216 | |
|
1217 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
1218 | 0 | /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
1219 | 0 | if (!args) { |
1220 | 0 | goto exit; |
1221 | 0 | } |
1222 | 0 | if (!noptargs) { |
1223 | 0 | goto skip_optional_pos; |
1224 | 0 | } |
1225 | 0 | keepends = PyObject_IsTrue(args[0]); |
1226 | 0 | if (keepends < 0) { |
1227 | 0 | goto exit; |
1228 | 0 | } |
1229 | 0 | skip_optional_pos: |
1230 | 0 | return_value = bytes_splitlines_impl((PyBytesObject *)self, keepends); |
1231 | |
|
1232 | 0 | exit: |
1233 | 0 | return return_value; |
1234 | 0 | } |
1235 | | |
1236 | | PyDoc_STRVAR(bytes_fromhex__doc__, |
1237 | | "fromhex($type, string, /)\n" |
1238 | | "--\n" |
1239 | | "\n" |
1240 | | "Create a bytes object from a string of hexadecimal numbers.\n" |
1241 | | "\n" |
1242 | | "Spaces between two numbers are accepted.\n" |
1243 | | "Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'."); |
1244 | | |
1245 | | #define BYTES_FROMHEX_METHODDEF \ |
1246 | | {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__}, |
1247 | | |
1248 | | static PyObject * |
1249 | | bytes_fromhex_impl(PyTypeObject *type, PyObject *string); |
1250 | | |
1251 | | static PyObject * |
1252 | | bytes_fromhex(PyObject *type, PyObject *string) |
1253 | 31.8k | { |
1254 | 31.8k | PyObject *return_value = NULL; |
1255 | | |
1256 | 31.8k | return_value = bytes_fromhex_impl((PyTypeObject *)type, string); |
1257 | | |
1258 | 31.8k | return return_value; |
1259 | 31.8k | } |
1260 | | |
1261 | | PyDoc_STRVAR(bytes_hex__doc__, |
1262 | | "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n" |
1263 | | "--\n" |
1264 | | "\n" |
1265 | | "Create a string of hexadecimal numbers from a bytes object.\n" |
1266 | | "\n" |
1267 | | " sep\n" |
1268 | | " An optional single character or byte to separate hex bytes.\n" |
1269 | | " bytes_per_sep\n" |
1270 | | " How many bytes between separators. Positive values count from the\n" |
1271 | | " right, negative values count from the left.\n" |
1272 | | "\n" |
1273 | | "Example:\n" |
1274 | | ">>> value = b\'\\xb9\\x01\\xef\'\n" |
1275 | | ">>> value.hex()\n" |
1276 | | "\'b901ef\'\n" |
1277 | | ">>> value.hex(\':\')\n" |
1278 | | "\'b9:01:ef\'\n" |
1279 | | ">>> value.hex(\':\', 2)\n" |
1280 | | "\'b9:01ef\'\n" |
1281 | | ">>> value.hex(\':\', -2)\n" |
1282 | | "\'b901:ef\'"); |
1283 | | |
1284 | | #define BYTES_HEX_METHODDEF \ |
1285 | | {"hex", _PyCFunction_CAST(bytes_hex), METH_FASTCALL|METH_KEYWORDS, bytes_hex__doc__}, |
1286 | | |
1287 | | static PyObject * |
1288 | | bytes_hex_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t bytes_per_sep); |
1289 | | |
1290 | | static PyObject * |
1291 | | bytes_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
1292 | 0 | { |
1293 | 0 | PyObject *return_value = NULL; |
1294 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
1295 | |
|
1296 | 0 | #define NUM_KEYWORDS 2 |
1297 | 0 | static struct { |
1298 | 0 | PyGC_Head _this_is_not_used; |
1299 | 0 | PyObject_VAR_HEAD |
1300 | 0 | Py_hash_t ob_hash; |
1301 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
1302 | 0 | } _kwtuple = { |
1303 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
1304 | 0 | .ob_hash = -1, |
1305 | 0 | .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), }, |
1306 | 0 | }; |
1307 | 0 | #undef NUM_KEYWORDS |
1308 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
1309 | |
|
1310 | | #else // !Py_BUILD_CORE |
1311 | | # define KWTUPLE NULL |
1312 | | #endif // !Py_BUILD_CORE |
1313 | |
|
1314 | 0 | static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL}; |
1315 | 0 | static _PyArg_Parser _parser = { |
1316 | 0 | .keywords = _keywords, |
1317 | 0 | .fname = "hex", |
1318 | 0 | .kwtuple = KWTUPLE, |
1319 | 0 | }; |
1320 | 0 | #undef KWTUPLE |
1321 | 0 | PyObject *argsbuf[2]; |
1322 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
1323 | 0 | PyObject *sep = NULL; |
1324 | 0 | Py_ssize_t bytes_per_sep = 1; |
1325 | |
|
1326 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
1327 | 0 | /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
1328 | 0 | if (!args) { |
1329 | 0 | goto exit; |
1330 | 0 | } |
1331 | 0 | if (!noptargs) { |
1332 | 0 | goto skip_optional_pos; |
1333 | 0 | } |
1334 | 0 | if (args[0]) { |
1335 | 0 | sep = args[0]; |
1336 | 0 | if (!--noptargs) { |
1337 | 0 | goto skip_optional_pos; |
1338 | 0 | } |
1339 | 0 | } |
1340 | 0 | { |
1341 | 0 | Py_ssize_t ival = -1; |
1342 | 0 | PyObject *iobj = _PyNumber_Index(args[1]); |
1343 | 0 | if (iobj != NULL) { |
1344 | 0 | ival = PyLong_AsSsize_t(iobj); |
1345 | 0 | Py_DECREF(iobj); |
1346 | 0 | } |
1347 | 0 | if (ival == -1 && PyErr_Occurred()) { |
1348 | 0 | goto exit; |
1349 | 0 | } |
1350 | 0 | bytes_per_sep = ival; |
1351 | 0 | } |
1352 | 0 | skip_optional_pos: |
1353 | 0 | return_value = bytes_hex_impl((PyBytesObject *)self, sep, bytes_per_sep); |
1354 | |
|
1355 | 0 | exit: |
1356 | 0 | return return_value; |
1357 | 0 | } |
1358 | | |
1359 | | static PyObject * |
1360 | | bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding, |
1361 | | const char *errors); |
1362 | | |
1363 | | static PyObject * |
1364 | | bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
1365 | 15.5M | { |
1366 | 15.5M | PyObject *return_value = NULL; |
1367 | 15.5M | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
1368 | | |
1369 | 15.5M | #define NUM_KEYWORDS 3 |
1370 | 15.5M | static struct { |
1371 | 15.5M | PyGC_Head _this_is_not_used; |
1372 | 15.5M | PyObject_VAR_HEAD |
1373 | 15.5M | Py_hash_t ob_hash; |
1374 | 15.5M | PyObject *ob_item[NUM_KEYWORDS]; |
1375 | 15.5M | } _kwtuple = { |
1376 | 15.5M | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
1377 | 15.5M | .ob_hash = -1, |
1378 | 15.5M | .ob_item = { &_Py_ID(source), &_Py_ID(encoding), &_Py_ID(errors), }, |
1379 | 15.5M | }; |
1380 | 15.5M | #undef NUM_KEYWORDS |
1381 | 15.5M | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
1382 | | |
1383 | | #else // !Py_BUILD_CORE |
1384 | | # define KWTUPLE NULL |
1385 | | #endif // !Py_BUILD_CORE |
1386 | | |
1387 | 15.5M | static const char * const _keywords[] = {"source", "encoding", "errors", NULL}; |
1388 | 15.5M | static _PyArg_Parser _parser = { |
1389 | 15.5M | .keywords = _keywords, |
1390 | 15.5M | .fname = "bytes", |
1391 | 15.5M | .kwtuple = KWTUPLE, |
1392 | 15.5M | }; |
1393 | 15.5M | #undef KWTUPLE |
1394 | 15.5M | PyObject *argsbuf[3]; |
1395 | 15.5M | PyObject * const *fastargs; |
1396 | 15.5M | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
1397 | 15.5M | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; |
1398 | 15.5M | PyObject *x = NULL; |
1399 | 15.5M | const char *encoding = NULL; |
1400 | 15.5M | const char *errors = NULL; |
1401 | | |
1402 | 15.5M | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, |
1403 | 15.5M | /*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
1404 | 15.5M | if (!fastargs) { |
1405 | 0 | goto exit; |
1406 | 0 | } |
1407 | 15.5M | if (!noptargs) { |
1408 | 0 | goto skip_optional_pos; |
1409 | 0 | } |
1410 | 15.5M | if (fastargs[0]) { |
1411 | 15.5M | x = fastargs[0]; |
1412 | 15.5M | if (!--noptargs) { |
1413 | 15.2M | goto skip_optional_pos; |
1414 | 15.2M | } |
1415 | 15.5M | } |
1416 | 364k | if (fastargs[1]) { |
1417 | 364k | if (!PyUnicode_Check(fastargs[1])) { |
1418 | 0 | _PyArg_BadArgument("bytes", "argument 'encoding'", "str", fastargs[1]); |
1419 | 0 | goto exit; |
1420 | 0 | } |
1421 | 364k | Py_ssize_t encoding_length; |
1422 | 364k | encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length); |
1423 | 364k | if (encoding == NULL) { |
1424 | 0 | goto exit; |
1425 | 0 | } |
1426 | 364k | if (strlen(encoding) != (size_t)encoding_length) { |
1427 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
1428 | 0 | goto exit; |
1429 | 0 | } |
1430 | 364k | if (!--noptargs) { |
1431 | 364k | goto skip_optional_pos; |
1432 | 364k | } |
1433 | 364k | } |
1434 | 0 | if (!PyUnicode_Check(fastargs[2])) { |
1435 | 0 | _PyArg_BadArgument("bytes", "argument 'errors'", "str", fastargs[2]); |
1436 | 0 | goto exit; |
1437 | 0 | } |
1438 | 0 | Py_ssize_t errors_length; |
1439 | 0 | errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length); |
1440 | 0 | if (errors == NULL) { |
1441 | 0 | goto exit; |
1442 | 0 | } |
1443 | 0 | if (strlen(errors) != (size_t)errors_length) { |
1444 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
1445 | 0 | goto exit; |
1446 | 0 | } |
1447 | 15.5M | skip_optional_pos: |
1448 | 15.5M | return_value = bytes_new_impl(type, x, encoding, errors); |
1449 | | |
1450 | 15.5M | exit: |
1451 | 15.5M | return return_value; |
1452 | 15.5M | } |
1453 | | /*[clinic end generated code: output=b252801ff04a89b3 input=a9049054013a1b77]*/ |