/src/cpython/Objects/clinic/bytearrayobject.c.h
Line | Count | Source (jump to first uncovered line) |
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_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() |
11 | | #include "pycore_modsupport.h" // _PyArg_UnpackKeywords() |
12 | | |
13 | | static int |
14 | | bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, |
15 | | const char *encoding, const char *errors); |
16 | | |
17 | | static int |
18 | | bytearray___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
19 | 936k | { |
20 | 936k | int return_value = -1; |
21 | 936k | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
22 | | |
23 | 936k | #define NUM_KEYWORDS 3 |
24 | 936k | static struct { |
25 | 936k | PyGC_Head _this_is_not_used; |
26 | 936k | PyObject_VAR_HEAD |
27 | 936k | Py_hash_t ob_hash; |
28 | 936k | PyObject *ob_item[NUM_KEYWORDS]; |
29 | 936k | } _kwtuple = { |
30 | 936k | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
31 | 936k | .ob_hash = -1, |
32 | 936k | .ob_item = { &_Py_ID(source), &_Py_ID(encoding), &_Py_ID(errors), }, |
33 | 936k | }; |
34 | 936k | #undef NUM_KEYWORDS |
35 | 936k | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
36 | | |
37 | | #else // !Py_BUILD_CORE |
38 | | # define KWTUPLE NULL |
39 | | #endif // !Py_BUILD_CORE |
40 | | |
41 | 936k | static const char * const _keywords[] = {"source", "encoding", "errors", NULL}; |
42 | 936k | static _PyArg_Parser _parser = { |
43 | 936k | .keywords = _keywords, |
44 | 936k | .fname = "bytearray", |
45 | 936k | .kwtuple = KWTUPLE, |
46 | 936k | }; |
47 | 936k | #undef KWTUPLE |
48 | 936k | PyObject *argsbuf[3]; |
49 | 936k | PyObject * const *fastargs; |
50 | 936k | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
51 | 936k | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; |
52 | 936k | PyObject *arg = NULL; |
53 | 936k | const char *encoding = NULL; |
54 | 936k | const char *errors = NULL; |
55 | | |
56 | 936k | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, |
57 | 936k | /*minpos*/ 0, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
58 | 936k | if (!fastargs) { |
59 | 0 | goto exit; |
60 | 0 | } |
61 | 936k | if (!noptargs) { |
62 | 416k | goto skip_optional_pos; |
63 | 416k | } |
64 | 519k | if (fastargs[0]) { |
65 | 519k | arg = fastargs[0]; |
66 | 519k | if (!--noptargs) { |
67 | 519k | goto skip_optional_pos; |
68 | 519k | } |
69 | 519k | } |
70 | 0 | if (fastargs[1]) { |
71 | 0 | if (!PyUnicode_Check(fastargs[1])) { |
72 | 0 | _PyArg_BadArgument("bytearray", "argument 'encoding'", "str", fastargs[1]); |
73 | 0 | goto exit; |
74 | 0 | } |
75 | 0 | Py_ssize_t encoding_length; |
76 | 0 | encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length); |
77 | 0 | if (encoding == NULL) { |
78 | 0 | goto exit; |
79 | 0 | } |
80 | 0 | if (strlen(encoding) != (size_t)encoding_length) { |
81 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
82 | 0 | goto exit; |
83 | 0 | } |
84 | 0 | if (!--noptargs) { |
85 | 0 | goto skip_optional_pos; |
86 | 0 | } |
87 | 0 | } |
88 | 0 | if (!PyUnicode_Check(fastargs[2])) { |
89 | 0 | _PyArg_BadArgument("bytearray", "argument 'errors'", "str", fastargs[2]); |
90 | 0 | goto exit; |
91 | 0 | } |
92 | 0 | Py_ssize_t errors_length; |
93 | 0 | errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length); |
94 | 0 | if (errors == NULL) { |
95 | 0 | goto exit; |
96 | 0 | } |
97 | 0 | if (strlen(errors) != (size_t)errors_length) { |
98 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
99 | 0 | goto exit; |
100 | 0 | } |
101 | 936k | skip_optional_pos: |
102 | 936k | return_value = bytearray___init___impl((PyByteArrayObject *)self, arg, encoding, errors); |
103 | | |
104 | 936k | exit: |
105 | 936k | return return_value; |
106 | 936k | } |
107 | | |
108 | | PyDoc_STRVAR(bytearray_find__doc__, |
109 | | "find($self, sub[, start[, end]], /)\n" |
110 | | "--\n" |
111 | | "\n" |
112 | | "Return the lowest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n" |
113 | | "\n" |
114 | | " start\n" |
115 | | " Optional start position. Default: start of the bytes.\n" |
116 | | " end\n" |
117 | | " Optional stop position. Default: end of the bytes.\n" |
118 | | "\n" |
119 | | "Return -1 on failure."); |
120 | | |
121 | | #define BYTEARRAY_FIND_METHODDEF \ |
122 | | {"find", _PyCFunction_CAST(bytearray_find), METH_FASTCALL, bytearray_find__doc__}, |
123 | | |
124 | | static PyObject * |
125 | | bytearray_find_impl(PyByteArrayObject *self, PyObject *sub, Py_ssize_t start, |
126 | | Py_ssize_t end); |
127 | | |
128 | | static PyObject * |
129 | | bytearray_find(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
130 | 1.59k | { |
131 | 1.59k | PyObject *return_value = NULL; |
132 | 1.59k | PyObject *sub; |
133 | 1.59k | Py_ssize_t start = 0; |
134 | 1.59k | Py_ssize_t end = PY_SSIZE_T_MAX; |
135 | | |
136 | 1.59k | if (!_PyArg_CheckPositional("find", nargs, 1, 3)) { |
137 | 0 | goto exit; |
138 | 0 | } |
139 | 1.59k | sub = args[0]; |
140 | 1.59k | if (nargs < 2) { |
141 | 0 | goto skip_optional; |
142 | 0 | } |
143 | 1.59k | if (!_PyEval_SliceIndex(args[1], &start)) { |
144 | 0 | goto exit; |
145 | 0 | } |
146 | 1.59k | if (nargs < 3) { |
147 | 1.59k | goto skip_optional; |
148 | 1.59k | } |
149 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
150 | 0 | goto exit; |
151 | 0 | } |
152 | 1.59k | skip_optional: |
153 | 1.59k | Py_BEGIN_CRITICAL_SECTION(self); |
154 | 1.59k | return_value = bytearray_find_impl((PyByteArrayObject *)self, sub, start, end); |
155 | 1.59k | Py_END_CRITICAL_SECTION(); |
156 | | |
157 | 1.59k | exit: |
158 | 1.59k | return return_value; |
159 | 1.59k | } |
160 | | |
161 | | PyDoc_STRVAR(bytearray_count__doc__, |
162 | | "count($self, sub[, start[, end]], /)\n" |
163 | | "--\n" |
164 | | "\n" |
165 | | "Return the number of non-overlapping occurrences of subsection \'sub\' in bytes B[start:end].\n" |
166 | | "\n" |
167 | | " start\n" |
168 | | " Optional start position. Default: start of the bytes.\n" |
169 | | " end\n" |
170 | | " Optional stop position. Default: end of the bytes."); |
171 | | |
172 | | #define BYTEARRAY_COUNT_METHODDEF \ |
173 | | {"count", _PyCFunction_CAST(bytearray_count), METH_FASTCALL, bytearray_count__doc__}, |
174 | | |
175 | | static PyObject * |
176 | | bytearray_count_impl(PyByteArrayObject *self, PyObject *sub, |
177 | | Py_ssize_t start, Py_ssize_t end); |
178 | | |
179 | | static PyObject * |
180 | | bytearray_count(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
181 | 0 | { |
182 | 0 | PyObject *return_value = NULL; |
183 | 0 | PyObject *sub; |
184 | 0 | Py_ssize_t start = 0; |
185 | 0 | Py_ssize_t end = PY_SSIZE_T_MAX; |
186 | |
|
187 | 0 | if (!_PyArg_CheckPositional("count", nargs, 1, 3)) { |
188 | 0 | goto exit; |
189 | 0 | } |
190 | 0 | sub = args[0]; |
191 | 0 | if (nargs < 2) { |
192 | 0 | goto skip_optional; |
193 | 0 | } |
194 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
195 | 0 | goto exit; |
196 | 0 | } |
197 | 0 | if (nargs < 3) { |
198 | 0 | goto skip_optional; |
199 | 0 | } |
200 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
201 | 0 | goto exit; |
202 | 0 | } |
203 | 0 | skip_optional: |
204 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
205 | 0 | return_value = bytearray_count_impl((PyByteArrayObject *)self, sub, start, end); |
206 | 0 | Py_END_CRITICAL_SECTION(); |
207 | |
|
208 | 0 | exit: |
209 | 0 | return return_value; |
210 | 0 | } |
211 | | |
212 | | PyDoc_STRVAR(bytearray_clear__doc__, |
213 | | "clear($self, /)\n" |
214 | | "--\n" |
215 | | "\n" |
216 | | "Remove all items from the bytearray."); |
217 | | |
218 | | #define BYTEARRAY_CLEAR_METHODDEF \ |
219 | | {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, bytearray_clear__doc__}, |
220 | | |
221 | | static PyObject * |
222 | | bytearray_clear_impl(PyByteArrayObject *self); |
223 | | |
224 | | static PyObject * |
225 | | bytearray_clear(PyObject *self, PyObject *Py_UNUSED(ignored)) |
226 | 0 | { |
227 | 0 | return bytearray_clear_impl((PyByteArrayObject *)self); |
228 | 0 | } |
229 | | |
230 | | PyDoc_STRVAR(bytearray_copy__doc__, |
231 | | "copy($self, /)\n" |
232 | | "--\n" |
233 | | "\n" |
234 | | "Return a copy of B."); |
235 | | |
236 | | #define BYTEARRAY_COPY_METHODDEF \ |
237 | | {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, bytearray_copy__doc__}, |
238 | | |
239 | | static PyObject * |
240 | | bytearray_copy_impl(PyByteArrayObject *self); |
241 | | |
242 | | static PyObject * |
243 | | bytearray_copy(PyObject *self, PyObject *Py_UNUSED(ignored)) |
244 | 0 | { |
245 | 0 | PyObject *return_value = NULL; |
246 | |
|
247 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
248 | 0 | return_value = bytearray_copy_impl((PyByteArrayObject *)self); |
249 | 0 | Py_END_CRITICAL_SECTION(); |
250 | |
|
251 | 0 | return return_value; |
252 | 0 | } |
253 | | |
254 | | PyDoc_STRVAR(bytearray_index__doc__, |
255 | | "index($self, sub[, start[, end]], /)\n" |
256 | | "--\n" |
257 | | "\n" |
258 | | "Return the lowest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n" |
259 | | "\n" |
260 | | " start\n" |
261 | | " Optional start position. Default: start of the bytes.\n" |
262 | | " end\n" |
263 | | " Optional stop position. Default: end of the bytes.\n" |
264 | | "\n" |
265 | | "Raise ValueError if the subsection is not found."); |
266 | | |
267 | | #define BYTEARRAY_INDEX_METHODDEF \ |
268 | | {"index", _PyCFunction_CAST(bytearray_index), METH_FASTCALL, bytearray_index__doc__}, |
269 | | |
270 | | static PyObject * |
271 | | bytearray_index_impl(PyByteArrayObject *self, PyObject *sub, |
272 | | Py_ssize_t start, Py_ssize_t end); |
273 | | |
274 | | static PyObject * |
275 | | bytearray_index(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
276 | 0 | { |
277 | 0 | PyObject *return_value = NULL; |
278 | 0 | PyObject *sub; |
279 | 0 | Py_ssize_t start = 0; |
280 | 0 | Py_ssize_t end = PY_SSIZE_T_MAX; |
281 | |
|
282 | 0 | if (!_PyArg_CheckPositional("index", nargs, 1, 3)) { |
283 | 0 | goto exit; |
284 | 0 | } |
285 | 0 | sub = args[0]; |
286 | 0 | if (nargs < 2) { |
287 | 0 | goto skip_optional; |
288 | 0 | } |
289 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
290 | 0 | goto exit; |
291 | 0 | } |
292 | 0 | if (nargs < 3) { |
293 | 0 | goto skip_optional; |
294 | 0 | } |
295 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
296 | 0 | goto exit; |
297 | 0 | } |
298 | 0 | skip_optional: |
299 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
300 | 0 | return_value = bytearray_index_impl((PyByteArrayObject *)self, sub, start, end); |
301 | 0 | Py_END_CRITICAL_SECTION(); |
302 | |
|
303 | 0 | exit: |
304 | 0 | return return_value; |
305 | 0 | } |
306 | | |
307 | | PyDoc_STRVAR(bytearray_rfind__doc__, |
308 | | "rfind($self, sub[, start[, end]], /)\n" |
309 | | "--\n" |
310 | | "\n" |
311 | | "Return the highest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n" |
312 | | "\n" |
313 | | " start\n" |
314 | | " Optional start position. Default: start of the bytes.\n" |
315 | | " end\n" |
316 | | " Optional stop position. Default: end of the bytes.\n" |
317 | | "\n" |
318 | | "Return -1 on failure."); |
319 | | |
320 | | #define BYTEARRAY_RFIND_METHODDEF \ |
321 | | {"rfind", _PyCFunction_CAST(bytearray_rfind), METH_FASTCALL, bytearray_rfind__doc__}, |
322 | | |
323 | | static PyObject * |
324 | | bytearray_rfind_impl(PyByteArrayObject *self, PyObject *sub, |
325 | | Py_ssize_t start, Py_ssize_t end); |
326 | | |
327 | | static PyObject * |
328 | | bytearray_rfind(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
329 | 0 | { |
330 | 0 | PyObject *return_value = NULL; |
331 | 0 | PyObject *sub; |
332 | 0 | Py_ssize_t start = 0; |
333 | 0 | Py_ssize_t end = PY_SSIZE_T_MAX; |
334 | |
|
335 | 0 | if (!_PyArg_CheckPositional("rfind", nargs, 1, 3)) { |
336 | 0 | goto exit; |
337 | 0 | } |
338 | 0 | sub = args[0]; |
339 | 0 | if (nargs < 2) { |
340 | 0 | goto skip_optional; |
341 | 0 | } |
342 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
343 | 0 | goto exit; |
344 | 0 | } |
345 | 0 | if (nargs < 3) { |
346 | 0 | goto skip_optional; |
347 | 0 | } |
348 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
349 | 0 | goto exit; |
350 | 0 | } |
351 | 0 | skip_optional: |
352 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
353 | 0 | return_value = bytearray_rfind_impl((PyByteArrayObject *)self, sub, start, end); |
354 | 0 | Py_END_CRITICAL_SECTION(); |
355 | |
|
356 | 0 | exit: |
357 | 0 | return return_value; |
358 | 0 | } |
359 | | |
360 | | PyDoc_STRVAR(bytearray_rindex__doc__, |
361 | | "rindex($self, sub[, start[, end]], /)\n" |
362 | | "--\n" |
363 | | "\n" |
364 | | "Return the highest index in B where subsection \'sub\' is found, such that \'sub\' is contained within B[start:end].\n" |
365 | | "\n" |
366 | | " start\n" |
367 | | " Optional start position. Default: start of the bytes.\n" |
368 | | " end\n" |
369 | | " Optional stop position. Default: end of the bytes.\n" |
370 | | "\n" |
371 | | "Raise ValueError if the subsection is not found."); |
372 | | |
373 | | #define BYTEARRAY_RINDEX_METHODDEF \ |
374 | | {"rindex", _PyCFunction_CAST(bytearray_rindex), METH_FASTCALL, bytearray_rindex__doc__}, |
375 | | |
376 | | static PyObject * |
377 | | bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub, |
378 | | Py_ssize_t start, Py_ssize_t end); |
379 | | |
380 | | static PyObject * |
381 | | bytearray_rindex(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
382 | 0 | { |
383 | 0 | PyObject *return_value = NULL; |
384 | 0 | PyObject *sub; |
385 | 0 | Py_ssize_t start = 0; |
386 | 0 | Py_ssize_t end = PY_SSIZE_T_MAX; |
387 | |
|
388 | 0 | if (!_PyArg_CheckPositional("rindex", nargs, 1, 3)) { |
389 | 0 | goto exit; |
390 | 0 | } |
391 | 0 | sub = args[0]; |
392 | 0 | if (nargs < 2) { |
393 | 0 | goto skip_optional; |
394 | 0 | } |
395 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
396 | 0 | goto exit; |
397 | 0 | } |
398 | 0 | if (nargs < 3) { |
399 | 0 | goto skip_optional; |
400 | 0 | } |
401 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
402 | 0 | goto exit; |
403 | 0 | } |
404 | 0 | skip_optional: |
405 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
406 | 0 | return_value = bytearray_rindex_impl((PyByteArrayObject *)self, sub, start, end); |
407 | 0 | Py_END_CRITICAL_SECTION(); |
408 | |
|
409 | 0 | exit: |
410 | 0 | return return_value; |
411 | 0 | } |
412 | | |
413 | | PyDoc_STRVAR(bytearray_startswith__doc__, |
414 | | "startswith($self, prefix[, start[, end]], /)\n" |
415 | | "--\n" |
416 | | "\n" |
417 | | "Return True if the bytearray starts with the specified prefix, False otherwise.\n" |
418 | | "\n" |
419 | | " prefix\n" |
420 | | " A bytes or a tuple of bytes to try.\n" |
421 | | " start\n" |
422 | | " Optional start position. Default: start of the bytearray.\n" |
423 | | " end\n" |
424 | | " Optional stop position. Default: end of the bytearray."); |
425 | | |
426 | | #define BYTEARRAY_STARTSWITH_METHODDEF \ |
427 | | {"startswith", _PyCFunction_CAST(bytearray_startswith), METH_FASTCALL, bytearray_startswith__doc__}, |
428 | | |
429 | | static PyObject * |
430 | | bytearray_startswith_impl(PyByteArrayObject *self, PyObject *subobj, |
431 | | Py_ssize_t start, Py_ssize_t end); |
432 | | |
433 | | static PyObject * |
434 | | bytearray_startswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
435 | 0 | { |
436 | 0 | PyObject *return_value = NULL; |
437 | 0 | PyObject *subobj; |
438 | 0 | Py_ssize_t start = 0; |
439 | 0 | Py_ssize_t end = PY_SSIZE_T_MAX; |
440 | |
|
441 | 0 | if (!_PyArg_CheckPositional("startswith", nargs, 1, 3)) { |
442 | 0 | goto exit; |
443 | 0 | } |
444 | 0 | subobj = args[0]; |
445 | 0 | if (nargs < 2) { |
446 | 0 | goto skip_optional; |
447 | 0 | } |
448 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
449 | 0 | goto exit; |
450 | 0 | } |
451 | 0 | if (nargs < 3) { |
452 | 0 | goto skip_optional; |
453 | 0 | } |
454 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
455 | 0 | goto exit; |
456 | 0 | } |
457 | 0 | skip_optional: |
458 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
459 | 0 | return_value = bytearray_startswith_impl((PyByteArrayObject *)self, subobj, start, end); |
460 | 0 | Py_END_CRITICAL_SECTION(); |
461 | |
|
462 | 0 | exit: |
463 | 0 | return return_value; |
464 | 0 | } |
465 | | |
466 | | PyDoc_STRVAR(bytearray_endswith__doc__, |
467 | | "endswith($self, suffix[, start[, end]], /)\n" |
468 | | "--\n" |
469 | | "\n" |
470 | | "Return True if the bytearray ends with the specified suffix, False otherwise.\n" |
471 | | "\n" |
472 | | " suffix\n" |
473 | | " A bytes or a tuple of bytes to try.\n" |
474 | | " start\n" |
475 | | " Optional start position. Default: start of the bytearray.\n" |
476 | | " end\n" |
477 | | " Optional stop position. Default: end of the bytearray."); |
478 | | |
479 | | #define BYTEARRAY_ENDSWITH_METHODDEF \ |
480 | | {"endswith", _PyCFunction_CAST(bytearray_endswith), METH_FASTCALL, bytearray_endswith__doc__}, |
481 | | |
482 | | static PyObject * |
483 | | bytearray_endswith_impl(PyByteArrayObject *self, PyObject *subobj, |
484 | | Py_ssize_t start, Py_ssize_t end); |
485 | | |
486 | | static PyObject * |
487 | | bytearray_endswith(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
488 | 0 | { |
489 | 0 | PyObject *return_value = NULL; |
490 | 0 | PyObject *subobj; |
491 | 0 | Py_ssize_t start = 0; |
492 | 0 | Py_ssize_t end = PY_SSIZE_T_MAX; |
493 | |
|
494 | 0 | if (!_PyArg_CheckPositional("endswith", nargs, 1, 3)) { |
495 | 0 | goto exit; |
496 | 0 | } |
497 | 0 | subobj = args[0]; |
498 | 0 | if (nargs < 2) { |
499 | 0 | goto skip_optional; |
500 | 0 | } |
501 | 0 | if (!_PyEval_SliceIndex(args[1], &start)) { |
502 | 0 | goto exit; |
503 | 0 | } |
504 | 0 | if (nargs < 3) { |
505 | 0 | goto skip_optional; |
506 | 0 | } |
507 | 0 | if (!_PyEval_SliceIndex(args[2], &end)) { |
508 | 0 | goto exit; |
509 | 0 | } |
510 | 0 | skip_optional: |
511 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
512 | 0 | return_value = bytearray_endswith_impl((PyByteArrayObject *)self, subobj, start, end); |
513 | 0 | Py_END_CRITICAL_SECTION(); |
514 | |
|
515 | 0 | exit: |
516 | 0 | return return_value; |
517 | 0 | } |
518 | | |
519 | | PyDoc_STRVAR(bytearray_removeprefix__doc__, |
520 | | "removeprefix($self, prefix, /)\n" |
521 | | "--\n" |
522 | | "\n" |
523 | | "Return a bytearray with the given prefix string removed if present.\n" |
524 | | "\n" |
525 | | "If the bytearray starts with the prefix string, return\n" |
526 | | "bytearray[len(prefix):]. Otherwise, return a copy of the original\n" |
527 | | "bytearray."); |
528 | | |
529 | | #define BYTEARRAY_REMOVEPREFIX_METHODDEF \ |
530 | | {"removeprefix", (PyCFunction)bytearray_removeprefix, METH_O, bytearray_removeprefix__doc__}, |
531 | | |
532 | | static PyObject * |
533 | | bytearray_removeprefix_impl(PyByteArrayObject *self, Py_buffer *prefix); |
534 | | |
535 | | static PyObject * |
536 | | bytearray_removeprefix(PyObject *self, PyObject *arg) |
537 | 0 | { |
538 | 0 | PyObject *return_value = NULL; |
539 | 0 | Py_buffer prefix = {NULL, NULL}; |
540 | |
|
541 | 0 | if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) { |
542 | 0 | goto exit; |
543 | 0 | } |
544 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
545 | 0 | return_value = bytearray_removeprefix_impl((PyByteArrayObject *)self, &prefix); |
546 | 0 | Py_END_CRITICAL_SECTION(); |
547 | |
|
548 | 0 | exit: |
549 | | /* Cleanup for prefix */ |
550 | 0 | if (prefix.obj) { |
551 | 0 | PyBuffer_Release(&prefix); |
552 | 0 | } |
553 | |
|
554 | 0 | return return_value; |
555 | 0 | } |
556 | | |
557 | | PyDoc_STRVAR(bytearray_removesuffix__doc__, |
558 | | "removesuffix($self, suffix, /)\n" |
559 | | "--\n" |
560 | | "\n" |
561 | | "Return a bytearray with the given suffix string removed if present.\n" |
562 | | "\n" |
563 | | "If the bytearray ends with the suffix string and that suffix is not\n" |
564 | | "empty, return bytearray[:-len(suffix)]. Otherwise, return a copy of\n" |
565 | | "the original bytearray."); |
566 | | |
567 | | #define BYTEARRAY_REMOVESUFFIX_METHODDEF \ |
568 | | {"removesuffix", (PyCFunction)bytearray_removesuffix, METH_O, bytearray_removesuffix__doc__}, |
569 | | |
570 | | static PyObject * |
571 | | bytearray_removesuffix_impl(PyByteArrayObject *self, Py_buffer *suffix); |
572 | | |
573 | | static PyObject * |
574 | | bytearray_removesuffix(PyObject *self, PyObject *arg) |
575 | 0 | { |
576 | 0 | PyObject *return_value = NULL; |
577 | 0 | Py_buffer suffix = {NULL, NULL}; |
578 | |
|
579 | 0 | if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) { |
580 | 0 | goto exit; |
581 | 0 | } |
582 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
583 | 0 | return_value = bytearray_removesuffix_impl((PyByteArrayObject *)self, &suffix); |
584 | 0 | Py_END_CRITICAL_SECTION(); |
585 | |
|
586 | 0 | exit: |
587 | | /* Cleanup for suffix */ |
588 | 0 | if (suffix.obj) { |
589 | 0 | PyBuffer_Release(&suffix); |
590 | 0 | } |
591 | |
|
592 | 0 | return return_value; |
593 | 0 | } |
594 | | |
595 | | PyDoc_STRVAR(bytearray_resize__doc__, |
596 | | "resize($self, size, /)\n" |
597 | | "--\n" |
598 | | "\n" |
599 | | "Resize the internal buffer of bytearray to len.\n" |
600 | | "\n" |
601 | | " size\n" |
602 | | " New size to resize to.."); |
603 | | |
604 | | #define BYTEARRAY_RESIZE_METHODDEF \ |
605 | | {"resize", (PyCFunction)bytearray_resize, METH_O, bytearray_resize__doc__}, |
606 | | |
607 | | static PyObject * |
608 | | bytearray_resize_impl(PyByteArrayObject *self, Py_ssize_t size); |
609 | | |
610 | | static PyObject * |
611 | | bytearray_resize(PyObject *self, PyObject *arg) |
612 | 0 | { |
613 | 0 | PyObject *return_value = NULL; |
614 | 0 | Py_ssize_t size; |
615 | |
|
616 | 0 | { |
617 | 0 | Py_ssize_t ival = -1; |
618 | 0 | PyObject *iobj = _PyNumber_Index(arg); |
619 | 0 | if (iobj != NULL) { |
620 | 0 | ival = PyLong_AsSsize_t(iobj); |
621 | 0 | Py_DECREF(iobj); |
622 | 0 | } |
623 | 0 | if (ival == -1 && PyErr_Occurred()) { |
624 | 0 | goto exit; |
625 | 0 | } |
626 | 0 | size = ival; |
627 | 0 | } |
628 | 0 | return_value = bytearray_resize_impl((PyByteArrayObject *)self, size); |
629 | |
|
630 | 0 | exit: |
631 | 0 | return return_value; |
632 | 0 | } |
633 | | |
634 | | PyDoc_STRVAR(bytearray_translate__doc__, |
635 | | "translate($self, table, /, delete=b\'\')\n" |
636 | | "--\n" |
637 | | "\n" |
638 | | "Return a copy with each character mapped by the given translation table.\n" |
639 | | "\n" |
640 | | " table\n" |
641 | | " Translation table, which must be a bytes object of length 256.\n" |
642 | | "\n" |
643 | | "All characters occurring in the optional argument delete are removed.\n" |
644 | | "The remaining characters are mapped through the given translation table."); |
645 | | |
646 | | #define BYTEARRAY_TRANSLATE_METHODDEF \ |
647 | | {"translate", _PyCFunction_CAST(bytearray_translate), METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__}, |
648 | | |
649 | | static PyObject * |
650 | | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, |
651 | | PyObject *deletechars); |
652 | | |
653 | | static PyObject * |
654 | | bytearray_translate(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
655 | 128 | { |
656 | 128 | PyObject *return_value = NULL; |
657 | 128 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
658 | | |
659 | 128 | #define NUM_KEYWORDS 1 |
660 | 128 | static struct { |
661 | 128 | PyGC_Head _this_is_not_used; |
662 | 128 | PyObject_VAR_HEAD |
663 | 128 | Py_hash_t ob_hash; |
664 | 128 | PyObject *ob_item[NUM_KEYWORDS]; |
665 | 128 | } _kwtuple = { |
666 | 128 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
667 | 128 | .ob_hash = -1, |
668 | 128 | .ob_item = { &_Py_ID(delete), }, |
669 | 128 | }; |
670 | 128 | #undef NUM_KEYWORDS |
671 | 128 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
672 | | |
673 | | #else // !Py_BUILD_CORE |
674 | | # define KWTUPLE NULL |
675 | | #endif // !Py_BUILD_CORE |
676 | | |
677 | 128 | static const char * const _keywords[] = {"", "delete", NULL}; |
678 | 128 | static _PyArg_Parser _parser = { |
679 | 128 | .keywords = _keywords, |
680 | 128 | .fname = "translate", |
681 | 128 | .kwtuple = KWTUPLE, |
682 | 128 | }; |
683 | 128 | #undef KWTUPLE |
684 | 128 | PyObject *argsbuf[2]; |
685 | 128 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
686 | 128 | PyObject *table; |
687 | 128 | PyObject *deletechars = NULL; |
688 | | |
689 | 128 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
690 | 128 | /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
691 | 128 | if (!args) { |
692 | 0 | goto exit; |
693 | 0 | } |
694 | 128 | table = args[0]; |
695 | 128 | if (!noptargs) { |
696 | 128 | goto skip_optional_pos; |
697 | 128 | } |
698 | 0 | deletechars = args[1]; |
699 | 128 | skip_optional_pos: |
700 | 128 | Py_BEGIN_CRITICAL_SECTION(self); |
701 | 128 | return_value = bytearray_translate_impl((PyByteArrayObject *)self, table, deletechars); |
702 | 128 | Py_END_CRITICAL_SECTION(); |
703 | | |
704 | 128 | exit: |
705 | 128 | return return_value; |
706 | 128 | } |
707 | | |
708 | | PyDoc_STRVAR(bytearray_maketrans__doc__, |
709 | | "maketrans(frm, to, /)\n" |
710 | | "--\n" |
711 | | "\n" |
712 | | "Return a translation table usable for the bytes or bytearray translate method.\n" |
713 | | "\n" |
714 | | "The returned table will be one where each byte in frm is mapped to the byte at\n" |
715 | | "the same position in to.\n" |
716 | | "\n" |
717 | | "The bytes objects frm and to must be of the same length."); |
718 | | |
719 | | #define BYTEARRAY_MAKETRANS_METHODDEF \ |
720 | | {"maketrans", _PyCFunction_CAST(bytearray_maketrans), METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__}, |
721 | | |
722 | | static PyObject * |
723 | | bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to); |
724 | | |
725 | | static PyObject * |
726 | | bytearray_maketrans(PyObject *null, PyObject *const *args, Py_ssize_t nargs) |
727 | 0 | { |
728 | 0 | PyObject *return_value = NULL; |
729 | 0 | Py_buffer frm = {NULL, NULL}; |
730 | 0 | Py_buffer to = {NULL, NULL}; |
731 | |
|
732 | 0 | if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) { |
733 | 0 | goto exit; |
734 | 0 | } |
735 | 0 | if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) { |
736 | 0 | goto exit; |
737 | 0 | } |
738 | 0 | if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) { |
739 | 0 | goto exit; |
740 | 0 | } |
741 | 0 | return_value = bytearray_maketrans_impl(&frm, &to); |
742 | |
|
743 | 0 | exit: |
744 | | /* Cleanup for frm */ |
745 | 0 | if (frm.obj) { |
746 | 0 | PyBuffer_Release(&frm); |
747 | 0 | } |
748 | | /* Cleanup for to */ |
749 | 0 | if (to.obj) { |
750 | 0 | PyBuffer_Release(&to); |
751 | 0 | } |
752 | |
|
753 | 0 | return return_value; |
754 | 0 | } |
755 | | |
756 | | PyDoc_STRVAR(bytearray_replace__doc__, |
757 | | "replace($self, old, new, count=-1, /)\n" |
758 | | "--\n" |
759 | | "\n" |
760 | | "Return a copy with all occurrences of substring old replaced by new.\n" |
761 | | "\n" |
762 | | " count\n" |
763 | | " Maximum number of occurrences to replace.\n" |
764 | | " -1 (the default value) means replace all occurrences.\n" |
765 | | "\n" |
766 | | "If the optional argument count is given, only the first count occurrences are\n" |
767 | | "replaced."); |
768 | | |
769 | | #define BYTEARRAY_REPLACE_METHODDEF \ |
770 | | {"replace", _PyCFunction_CAST(bytearray_replace), METH_FASTCALL, bytearray_replace__doc__}, |
771 | | |
772 | | static PyObject * |
773 | | bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, |
774 | | Py_buffer *new, Py_ssize_t count); |
775 | | |
776 | | static PyObject * |
777 | | bytearray_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
778 | 0 | { |
779 | 0 | PyObject *return_value = NULL; |
780 | 0 | Py_buffer old = {NULL, NULL}; |
781 | 0 | Py_buffer new = {NULL, NULL}; |
782 | 0 | Py_ssize_t count = -1; |
783 | |
|
784 | 0 | if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) { |
785 | 0 | goto exit; |
786 | 0 | } |
787 | 0 | if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) { |
788 | 0 | goto exit; |
789 | 0 | } |
790 | 0 | if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) { |
791 | 0 | goto exit; |
792 | 0 | } |
793 | 0 | if (nargs < 3) { |
794 | 0 | goto skip_optional; |
795 | 0 | } |
796 | 0 | { |
797 | 0 | Py_ssize_t ival = -1; |
798 | 0 | PyObject *iobj = _PyNumber_Index(args[2]); |
799 | 0 | if (iobj != NULL) { |
800 | 0 | ival = PyLong_AsSsize_t(iobj); |
801 | 0 | Py_DECREF(iobj); |
802 | 0 | } |
803 | 0 | if (ival == -1 && PyErr_Occurred()) { |
804 | 0 | goto exit; |
805 | 0 | } |
806 | 0 | count = ival; |
807 | 0 | } |
808 | 0 | skip_optional: |
809 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
810 | 0 | return_value = bytearray_replace_impl((PyByteArrayObject *)self, &old, &new, count); |
811 | 0 | Py_END_CRITICAL_SECTION(); |
812 | |
|
813 | 0 | exit: |
814 | | /* Cleanup for old */ |
815 | 0 | if (old.obj) { |
816 | 0 | PyBuffer_Release(&old); |
817 | 0 | } |
818 | | /* Cleanup for new */ |
819 | 0 | if (new.obj) { |
820 | 0 | PyBuffer_Release(&new); |
821 | 0 | } |
822 | |
|
823 | 0 | return return_value; |
824 | 0 | } |
825 | | |
826 | | PyDoc_STRVAR(bytearray_split__doc__, |
827 | | "split($self, /, sep=None, maxsplit=-1)\n" |
828 | | "--\n" |
829 | | "\n" |
830 | | "Return a list of the sections in the bytearray, using sep as the delimiter.\n" |
831 | | "\n" |
832 | | " sep\n" |
833 | | " The delimiter according which to split the bytearray.\n" |
834 | | " None (the default value) means split on ASCII whitespace characters\n" |
835 | | " (space, tab, return, newline, formfeed, vertical tab).\n" |
836 | | " maxsplit\n" |
837 | | " Maximum number of splits to do.\n" |
838 | | " -1 (the default value) means no limit."); |
839 | | |
840 | | #define BYTEARRAY_SPLIT_METHODDEF \ |
841 | | {"split", _PyCFunction_CAST(bytearray_split), METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__}, |
842 | | |
843 | | static PyObject * |
844 | | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, |
845 | | Py_ssize_t maxsplit); |
846 | | |
847 | | static PyObject * |
848 | | bytearray_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
849 | 0 | { |
850 | 0 | PyObject *return_value = NULL; |
851 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
852 | |
|
853 | 0 | #define NUM_KEYWORDS 2 |
854 | 0 | static struct { |
855 | 0 | PyGC_Head _this_is_not_used; |
856 | 0 | PyObject_VAR_HEAD |
857 | 0 | Py_hash_t ob_hash; |
858 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
859 | 0 | } _kwtuple = { |
860 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
861 | 0 | .ob_hash = -1, |
862 | 0 | .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), }, |
863 | 0 | }; |
864 | 0 | #undef NUM_KEYWORDS |
865 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
866 | |
|
867 | | #else // !Py_BUILD_CORE |
868 | | # define KWTUPLE NULL |
869 | | #endif // !Py_BUILD_CORE |
870 | |
|
871 | 0 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
872 | 0 | static _PyArg_Parser _parser = { |
873 | 0 | .keywords = _keywords, |
874 | 0 | .fname = "split", |
875 | 0 | .kwtuple = KWTUPLE, |
876 | 0 | }; |
877 | 0 | #undef KWTUPLE |
878 | 0 | PyObject *argsbuf[2]; |
879 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
880 | 0 | PyObject *sep = Py_None; |
881 | 0 | Py_ssize_t maxsplit = -1; |
882 | |
|
883 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
884 | 0 | /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
885 | 0 | if (!args) { |
886 | 0 | goto exit; |
887 | 0 | } |
888 | 0 | if (!noptargs) { |
889 | 0 | goto skip_optional_pos; |
890 | 0 | } |
891 | 0 | if (args[0]) { |
892 | 0 | sep = args[0]; |
893 | 0 | if (!--noptargs) { |
894 | 0 | goto skip_optional_pos; |
895 | 0 | } |
896 | 0 | } |
897 | 0 | { |
898 | 0 | Py_ssize_t ival = -1; |
899 | 0 | PyObject *iobj = _PyNumber_Index(args[1]); |
900 | 0 | if (iobj != NULL) { |
901 | 0 | ival = PyLong_AsSsize_t(iobj); |
902 | 0 | Py_DECREF(iobj); |
903 | 0 | } |
904 | 0 | if (ival == -1 && PyErr_Occurred()) { |
905 | 0 | goto exit; |
906 | 0 | } |
907 | 0 | maxsplit = ival; |
908 | 0 | } |
909 | 0 | skip_optional_pos: |
910 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
911 | 0 | return_value = bytearray_split_impl((PyByteArrayObject *)self, sep, maxsplit); |
912 | 0 | Py_END_CRITICAL_SECTION(); |
913 | |
|
914 | 0 | exit: |
915 | 0 | return return_value; |
916 | 0 | } |
917 | | |
918 | | PyDoc_STRVAR(bytearray_partition__doc__, |
919 | | "partition($self, sep, /)\n" |
920 | | "--\n" |
921 | | "\n" |
922 | | "Partition the bytearray into three parts using the given separator.\n" |
923 | | "\n" |
924 | | "This will search for the separator sep in the bytearray. If the separator is\n" |
925 | | "found, returns a 3-tuple containing the part before the separator, the\n" |
926 | | "separator itself, and the part after it as new bytearray objects.\n" |
927 | | "\n" |
928 | | "If the separator is not found, returns a 3-tuple containing the copy of the\n" |
929 | | "original bytearray object and two empty bytearray objects."); |
930 | | |
931 | | #define BYTEARRAY_PARTITION_METHODDEF \ |
932 | | {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__}, |
933 | | |
934 | | static PyObject * |
935 | | bytearray_partition_impl(PyByteArrayObject *self, PyObject *sep); |
936 | | |
937 | | static PyObject * |
938 | | bytearray_partition(PyObject *self, PyObject *sep) |
939 | 0 | { |
940 | 0 | PyObject *return_value = NULL; |
941 | |
|
942 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
943 | 0 | return_value = bytearray_partition_impl((PyByteArrayObject *)self, sep); |
944 | 0 | Py_END_CRITICAL_SECTION(); |
945 | |
|
946 | 0 | return return_value; |
947 | 0 | } |
948 | | |
949 | | PyDoc_STRVAR(bytearray_rpartition__doc__, |
950 | | "rpartition($self, sep, /)\n" |
951 | | "--\n" |
952 | | "\n" |
953 | | "Partition the bytearray into three parts using the given separator.\n" |
954 | | "\n" |
955 | | "This will search for the separator sep in the bytearray, starting at the end.\n" |
956 | | "If the separator is found, returns a 3-tuple containing the part before the\n" |
957 | | "separator, the separator itself, and the part after it as new bytearray\n" |
958 | | "objects.\n" |
959 | | "\n" |
960 | | "If the separator is not found, returns a 3-tuple containing two empty bytearray\n" |
961 | | "objects and the copy of the original bytearray object."); |
962 | | |
963 | | #define BYTEARRAY_RPARTITION_METHODDEF \ |
964 | | {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__}, |
965 | | |
966 | | static PyObject * |
967 | | bytearray_rpartition_impl(PyByteArrayObject *self, PyObject *sep); |
968 | | |
969 | | static PyObject * |
970 | | bytearray_rpartition(PyObject *self, PyObject *sep) |
971 | 0 | { |
972 | 0 | PyObject *return_value = NULL; |
973 | |
|
974 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
975 | 0 | return_value = bytearray_rpartition_impl((PyByteArrayObject *)self, sep); |
976 | 0 | Py_END_CRITICAL_SECTION(); |
977 | |
|
978 | 0 | return return_value; |
979 | 0 | } |
980 | | |
981 | | PyDoc_STRVAR(bytearray_rsplit__doc__, |
982 | | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
983 | | "--\n" |
984 | | "\n" |
985 | | "Return a list of the sections in the bytearray, using sep as the delimiter.\n" |
986 | | "\n" |
987 | | " sep\n" |
988 | | " The delimiter according which to split the bytearray.\n" |
989 | | " None (the default value) means split on ASCII whitespace characters\n" |
990 | | " (space, tab, return, newline, formfeed, vertical tab).\n" |
991 | | " maxsplit\n" |
992 | | " Maximum number of splits to do.\n" |
993 | | " -1 (the default value) means no limit.\n" |
994 | | "\n" |
995 | | "Splitting is done starting at the end of the bytearray and working to the front."); |
996 | | |
997 | | #define BYTEARRAY_RSPLIT_METHODDEF \ |
998 | | {"rsplit", _PyCFunction_CAST(bytearray_rsplit), METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__}, |
999 | | |
1000 | | static PyObject * |
1001 | | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, |
1002 | | Py_ssize_t maxsplit); |
1003 | | |
1004 | | static PyObject * |
1005 | | bytearray_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
1006 | 0 | { |
1007 | 0 | PyObject *return_value = NULL; |
1008 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
1009 | |
|
1010 | 0 | #define NUM_KEYWORDS 2 |
1011 | 0 | static struct { |
1012 | 0 | PyGC_Head _this_is_not_used; |
1013 | 0 | PyObject_VAR_HEAD |
1014 | 0 | Py_hash_t ob_hash; |
1015 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
1016 | 0 | } _kwtuple = { |
1017 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
1018 | 0 | .ob_hash = -1, |
1019 | 0 | .ob_item = { &_Py_ID(sep), &_Py_ID(maxsplit), }, |
1020 | 0 | }; |
1021 | 0 | #undef NUM_KEYWORDS |
1022 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
1023 | |
|
1024 | | #else // !Py_BUILD_CORE |
1025 | | # define KWTUPLE NULL |
1026 | | #endif // !Py_BUILD_CORE |
1027 | |
|
1028 | 0 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
1029 | 0 | static _PyArg_Parser _parser = { |
1030 | 0 | .keywords = _keywords, |
1031 | 0 | .fname = "rsplit", |
1032 | 0 | .kwtuple = KWTUPLE, |
1033 | 0 | }; |
1034 | 0 | #undef KWTUPLE |
1035 | 0 | PyObject *argsbuf[2]; |
1036 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
1037 | 0 | PyObject *sep = Py_None; |
1038 | 0 | Py_ssize_t maxsplit = -1; |
1039 | |
|
1040 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
1041 | 0 | /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
1042 | 0 | if (!args) { |
1043 | 0 | goto exit; |
1044 | 0 | } |
1045 | 0 | if (!noptargs) { |
1046 | 0 | goto skip_optional_pos; |
1047 | 0 | } |
1048 | 0 | if (args[0]) { |
1049 | 0 | sep = args[0]; |
1050 | 0 | if (!--noptargs) { |
1051 | 0 | goto skip_optional_pos; |
1052 | 0 | } |
1053 | 0 | } |
1054 | 0 | { |
1055 | 0 | Py_ssize_t ival = -1; |
1056 | 0 | PyObject *iobj = _PyNumber_Index(args[1]); |
1057 | 0 | if (iobj != NULL) { |
1058 | 0 | ival = PyLong_AsSsize_t(iobj); |
1059 | 0 | Py_DECREF(iobj); |
1060 | 0 | } |
1061 | 0 | if (ival == -1 && PyErr_Occurred()) { |
1062 | 0 | goto exit; |
1063 | 0 | } |
1064 | 0 | maxsplit = ival; |
1065 | 0 | } |
1066 | 0 | skip_optional_pos: |
1067 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1068 | 0 | return_value = bytearray_rsplit_impl((PyByteArrayObject *)self, sep, maxsplit); |
1069 | 0 | Py_END_CRITICAL_SECTION(); |
1070 | |
|
1071 | 0 | exit: |
1072 | 0 | return return_value; |
1073 | 0 | } |
1074 | | |
1075 | | PyDoc_STRVAR(bytearray_reverse__doc__, |
1076 | | "reverse($self, /)\n" |
1077 | | "--\n" |
1078 | | "\n" |
1079 | | "Reverse the order of the values in B in place."); |
1080 | | |
1081 | | #define BYTEARRAY_REVERSE_METHODDEF \ |
1082 | | {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__}, |
1083 | | |
1084 | | static PyObject * |
1085 | | bytearray_reverse_impl(PyByteArrayObject *self); |
1086 | | |
1087 | | static PyObject * |
1088 | | bytearray_reverse(PyObject *self, PyObject *Py_UNUSED(ignored)) |
1089 | 0 | { |
1090 | 0 | PyObject *return_value = NULL; |
1091 | |
|
1092 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1093 | 0 | return_value = bytearray_reverse_impl((PyByteArrayObject *)self); |
1094 | 0 | Py_END_CRITICAL_SECTION(); |
1095 | |
|
1096 | 0 | return return_value; |
1097 | 0 | } |
1098 | | |
1099 | | PyDoc_STRVAR(bytearray_insert__doc__, |
1100 | | "insert($self, index, item, /)\n" |
1101 | | "--\n" |
1102 | | "\n" |
1103 | | "Insert a single item into the bytearray before the given index.\n" |
1104 | | "\n" |
1105 | | " index\n" |
1106 | | " The index where the value is to be inserted.\n" |
1107 | | " item\n" |
1108 | | " The item to be inserted."); |
1109 | | |
1110 | | #define BYTEARRAY_INSERT_METHODDEF \ |
1111 | | {"insert", _PyCFunction_CAST(bytearray_insert), METH_FASTCALL, bytearray_insert__doc__}, |
1112 | | |
1113 | | static PyObject * |
1114 | | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item); |
1115 | | |
1116 | | static PyObject * |
1117 | | bytearray_insert(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
1118 | 0 | { |
1119 | 0 | PyObject *return_value = NULL; |
1120 | 0 | Py_ssize_t index; |
1121 | 0 | int item; |
1122 | |
|
1123 | 0 | if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) { |
1124 | 0 | goto exit; |
1125 | 0 | } |
1126 | 0 | { |
1127 | 0 | Py_ssize_t ival = -1; |
1128 | 0 | PyObject *iobj = _PyNumber_Index(args[0]); |
1129 | 0 | if (iobj != NULL) { |
1130 | 0 | ival = PyLong_AsSsize_t(iobj); |
1131 | 0 | Py_DECREF(iobj); |
1132 | 0 | } |
1133 | 0 | if (ival == -1 && PyErr_Occurred()) { |
1134 | 0 | goto exit; |
1135 | 0 | } |
1136 | 0 | index = ival; |
1137 | 0 | } |
1138 | 0 | if (!_getbytevalue(args[1], &item)) { |
1139 | 0 | goto exit; |
1140 | 0 | } |
1141 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1142 | 0 | return_value = bytearray_insert_impl((PyByteArrayObject *)self, index, item); |
1143 | 0 | Py_END_CRITICAL_SECTION(); |
1144 | |
|
1145 | 0 | exit: |
1146 | 0 | return return_value; |
1147 | 0 | } |
1148 | | |
1149 | | PyDoc_STRVAR(bytearray_append__doc__, |
1150 | | "append($self, item, /)\n" |
1151 | | "--\n" |
1152 | | "\n" |
1153 | | "Append a single item to the end of the bytearray.\n" |
1154 | | "\n" |
1155 | | " item\n" |
1156 | | " The item to be appended."); |
1157 | | |
1158 | | #define BYTEARRAY_APPEND_METHODDEF \ |
1159 | | {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__}, |
1160 | | |
1161 | | static PyObject * |
1162 | | bytearray_append_impl(PyByteArrayObject *self, int item); |
1163 | | |
1164 | | static PyObject * |
1165 | | bytearray_append(PyObject *self, PyObject *arg) |
1166 | 580k | { |
1167 | 580k | PyObject *return_value = NULL; |
1168 | 580k | int item; |
1169 | | |
1170 | 580k | if (!_getbytevalue(arg, &item)) { |
1171 | 0 | goto exit; |
1172 | 0 | } |
1173 | 580k | Py_BEGIN_CRITICAL_SECTION(self); |
1174 | 580k | return_value = bytearray_append_impl((PyByteArrayObject *)self, item); |
1175 | 580k | Py_END_CRITICAL_SECTION(); |
1176 | | |
1177 | 580k | exit: |
1178 | 580k | return return_value; |
1179 | 580k | } |
1180 | | |
1181 | | PyDoc_STRVAR(bytearray_extend__doc__, |
1182 | | "extend($self, iterable_of_ints, /)\n" |
1183 | | "--\n" |
1184 | | "\n" |
1185 | | "Append all the items from the iterator or sequence to the end of the bytearray.\n" |
1186 | | "\n" |
1187 | | " iterable_of_ints\n" |
1188 | | " The iterable of items to append."); |
1189 | | |
1190 | | #define BYTEARRAY_EXTEND_METHODDEF \ |
1191 | | {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__}, |
1192 | | |
1193 | | static PyObject * |
1194 | | bytearray_extend_impl(PyByteArrayObject *self, PyObject *iterable_of_ints); |
1195 | | |
1196 | | static PyObject * |
1197 | | bytearray_extend(PyObject *self, PyObject *iterable_of_ints) |
1198 | 8.66M | { |
1199 | 8.66M | PyObject *return_value = NULL; |
1200 | | |
1201 | 8.66M | Py_BEGIN_CRITICAL_SECTION(self); |
1202 | 8.66M | return_value = bytearray_extend_impl((PyByteArrayObject *)self, iterable_of_ints); |
1203 | 8.66M | Py_END_CRITICAL_SECTION(); |
1204 | | |
1205 | 8.66M | return return_value; |
1206 | 8.66M | } |
1207 | | |
1208 | | PyDoc_STRVAR(bytearray_pop__doc__, |
1209 | | "pop($self, index=-1, /)\n" |
1210 | | "--\n" |
1211 | | "\n" |
1212 | | "Remove and return a single item from B.\n" |
1213 | | "\n" |
1214 | | " index\n" |
1215 | | " The index from where to remove the item.\n" |
1216 | | " -1 (the default value) means remove the last item.\n" |
1217 | | "\n" |
1218 | | "If no index argument is given, will pop the last item."); |
1219 | | |
1220 | | #define BYTEARRAY_POP_METHODDEF \ |
1221 | | {"pop", _PyCFunction_CAST(bytearray_pop), METH_FASTCALL, bytearray_pop__doc__}, |
1222 | | |
1223 | | static PyObject * |
1224 | | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index); |
1225 | | |
1226 | | static PyObject * |
1227 | | bytearray_pop(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
1228 | 0 | { |
1229 | 0 | PyObject *return_value = NULL; |
1230 | 0 | Py_ssize_t index = -1; |
1231 | |
|
1232 | 0 | if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) { |
1233 | 0 | goto exit; |
1234 | 0 | } |
1235 | 0 | if (nargs < 1) { |
1236 | 0 | goto skip_optional; |
1237 | 0 | } |
1238 | 0 | { |
1239 | 0 | Py_ssize_t ival = -1; |
1240 | 0 | PyObject *iobj = _PyNumber_Index(args[0]); |
1241 | 0 | if (iobj != NULL) { |
1242 | 0 | ival = PyLong_AsSsize_t(iobj); |
1243 | 0 | Py_DECREF(iobj); |
1244 | 0 | } |
1245 | 0 | if (ival == -1 && PyErr_Occurred()) { |
1246 | 0 | goto exit; |
1247 | 0 | } |
1248 | 0 | index = ival; |
1249 | 0 | } |
1250 | 0 | skip_optional: |
1251 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1252 | 0 | return_value = bytearray_pop_impl((PyByteArrayObject *)self, index); |
1253 | 0 | Py_END_CRITICAL_SECTION(); |
1254 | |
|
1255 | 0 | exit: |
1256 | 0 | return return_value; |
1257 | 0 | } |
1258 | | |
1259 | | PyDoc_STRVAR(bytearray_remove__doc__, |
1260 | | "remove($self, value, /)\n" |
1261 | | "--\n" |
1262 | | "\n" |
1263 | | "Remove the first occurrence of a value in the bytearray.\n" |
1264 | | "\n" |
1265 | | " value\n" |
1266 | | " The value to remove."); |
1267 | | |
1268 | | #define BYTEARRAY_REMOVE_METHODDEF \ |
1269 | | {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__}, |
1270 | | |
1271 | | static PyObject * |
1272 | | bytearray_remove_impl(PyByteArrayObject *self, int value); |
1273 | | |
1274 | | static PyObject * |
1275 | | bytearray_remove(PyObject *self, PyObject *arg) |
1276 | 0 | { |
1277 | 0 | PyObject *return_value = NULL; |
1278 | 0 | int value; |
1279 | |
|
1280 | 0 | if (!_getbytevalue(arg, &value)) { |
1281 | 0 | goto exit; |
1282 | 0 | } |
1283 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1284 | 0 | return_value = bytearray_remove_impl((PyByteArrayObject *)self, value); |
1285 | 0 | Py_END_CRITICAL_SECTION(); |
1286 | |
|
1287 | 0 | exit: |
1288 | 0 | return return_value; |
1289 | 0 | } |
1290 | | |
1291 | | PyDoc_STRVAR(bytearray_strip__doc__, |
1292 | | "strip($self, bytes=None, /)\n" |
1293 | | "--\n" |
1294 | | "\n" |
1295 | | "Strip leading and trailing bytes contained in the argument.\n" |
1296 | | "\n" |
1297 | | "If the argument is omitted or None, strip leading and trailing ASCII whitespace."); |
1298 | | |
1299 | | #define BYTEARRAY_STRIP_METHODDEF \ |
1300 | | {"strip", _PyCFunction_CAST(bytearray_strip), METH_FASTCALL, bytearray_strip__doc__}, |
1301 | | |
1302 | | static PyObject * |
1303 | | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes); |
1304 | | |
1305 | | static PyObject * |
1306 | | bytearray_strip(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
1307 | 0 | { |
1308 | 0 | PyObject *return_value = NULL; |
1309 | 0 | PyObject *bytes = Py_None; |
1310 | |
|
1311 | 0 | if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) { |
1312 | 0 | goto exit; |
1313 | 0 | } |
1314 | 0 | if (nargs < 1) { |
1315 | 0 | goto skip_optional; |
1316 | 0 | } |
1317 | 0 | bytes = args[0]; |
1318 | 0 | skip_optional: |
1319 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1320 | 0 | return_value = bytearray_strip_impl((PyByteArrayObject *)self, bytes); |
1321 | 0 | Py_END_CRITICAL_SECTION(); |
1322 | |
|
1323 | 0 | exit: |
1324 | 0 | return return_value; |
1325 | 0 | } |
1326 | | |
1327 | | PyDoc_STRVAR(bytearray_lstrip__doc__, |
1328 | | "lstrip($self, bytes=None, /)\n" |
1329 | | "--\n" |
1330 | | "\n" |
1331 | | "Strip leading bytes contained in the argument.\n" |
1332 | | "\n" |
1333 | | "If the argument is omitted or None, strip leading ASCII whitespace."); |
1334 | | |
1335 | | #define BYTEARRAY_LSTRIP_METHODDEF \ |
1336 | | {"lstrip", _PyCFunction_CAST(bytearray_lstrip), METH_FASTCALL, bytearray_lstrip__doc__}, |
1337 | | |
1338 | | static PyObject * |
1339 | | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes); |
1340 | | |
1341 | | static PyObject * |
1342 | | bytearray_lstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
1343 | 0 | { |
1344 | 0 | PyObject *return_value = NULL; |
1345 | 0 | PyObject *bytes = Py_None; |
1346 | |
|
1347 | 0 | if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) { |
1348 | 0 | goto exit; |
1349 | 0 | } |
1350 | 0 | if (nargs < 1) { |
1351 | 0 | goto skip_optional; |
1352 | 0 | } |
1353 | 0 | bytes = args[0]; |
1354 | 0 | skip_optional: |
1355 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1356 | 0 | return_value = bytearray_lstrip_impl((PyByteArrayObject *)self, bytes); |
1357 | 0 | Py_END_CRITICAL_SECTION(); |
1358 | |
|
1359 | 0 | exit: |
1360 | 0 | return return_value; |
1361 | 0 | } |
1362 | | |
1363 | | PyDoc_STRVAR(bytearray_rstrip__doc__, |
1364 | | "rstrip($self, bytes=None, /)\n" |
1365 | | "--\n" |
1366 | | "\n" |
1367 | | "Strip trailing bytes contained in the argument.\n" |
1368 | | "\n" |
1369 | | "If the argument is omitted or None, strip trailing ASCII whitespace."); |
1370 | | |
1371 | | #define BYTEARRAY_RSTRIP_METHODDEF \ |
1372 | | {"rstrip", _PyCFunction_CAST(bytearray_rstrip), METH_FASTCALL, bytearray_rstrip__doc__}, |
1373 | | |
1374 | | static PyObject * |
1375 | | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes); |
1376 | | |
1377 | | static PyObject * |
1378 | | bytearray_rstrip(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
1379 | 0 | { |
1380 | 0 | PyObject *return_value = NULL; |
1381 | 0 | PyObject *bytes = Py_None; |
1382 | |
|
1383 | 0 | if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) { |
1384 | 0 | goto exit; |
1385 | 0 | } |
1386 | 0 | if (nargs < 1) { |
1387 | 0 | goto skip_optional; |
1388 | 0 | } |
1389 | 0 | bytes = args[0]; |
1390 | 0 | skip_optional: |
1391 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1392 | 0 | return_value = bytearray_rstrip_impl((PyByteArrayObject *)self, bytes); |
1393 | 0 | Py_END_CRITICAL_SECTION(); |
1394 | |
|
1395 | 0 | exit: |
1396 | 0 | return return_value; |
1397 | 0 | } |
1398 | | |
1399 | | PyDoc_STRVAR(bytearray_decode__doc__, |
1400 | | "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
1401 | | "--\n" |
1402 | | "\n" |
1403 | | "Decode the bytearray using the codec registered for encoding.\n" |
1404 | | "\n" |
1405 | | " encoding\n" |
1406 | | " The encoding with which to decode the bytearray.\n" |
1407 | | " errors\n" |
1408 | | " The error handling scheme to use for the handling of decoding errors.\n" |
1409 | | " The default is \'strict\' meaning that decoding errors raise a\n" |
1410 | | " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n" |
1411 | | " as well as any other name registered with codecs.register_error that\n" |
1412 | | " can handle UnicodeDecodeErrors."); |
1413 | | |
1414 | | #define BYTEARRAY_DECODE_METHODDEF \ |
1415 | | {"decode", _PyCFunction_CAST(bytearray_decode), METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__}, |
1416 | | |
1417 | | static PyObject * |
1418 | | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, |
1419 | | const char *errors); |
1420 | | |
1421 | | static PyObject * |
1422 | | bytearray_decode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
1423 | 485k | { |
1424 | 485k | PyObject *return_value = NULL; |
1425 | 485k | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
1426 | | |
1427 | 485k | #define NUM_KEYWORDS 2 |
1428 | 485k | static struct { |
1429 | 485k | PyGC_Head _this_is_not_used; |
1430 | 485k | PyObject_VAR_HEAD |
1431 | 485k | Py_hash_t ob_hash; |
1432 | 485k | PyObject *ob_item[NUM_KEYWORDS]; |
1433 | 485k | } _kwtuple = { |
1434 | 485k | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
1435 | 485k | .ob_hash = -1, |
1436 | 485k | .ob_item = { &_Py_ID(encoding), &_Py_ID(errors), }, |
1437 | 485k | }; |
1438 | 485k | #undef NUM_KEYWORDS |
1439 | 485k | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
1440 | | |
1441 | | #else // !Py_BUILD_CORE |
1442 | | # define KWTUPLE NULL |
1443 | | #endif // !Py_BUILD_CORE |
1444 | | |
1445 | 485k | static const char * const _keywords[] = {"encoding", "errors", NULL}; |
1446 | 485k | static _PyArg_Parser _parser = { |
1447 | 485k | .keywords = _keywords, |
1448 | 485k | .fname = "decode", |
1449 | 485k | .kwtuple = KWTUPLE, |
1450 | 485k | }; |
1451 | 485k | #undef KWTUPLE |
1452 | 485k | PyObject *argsbuf[2]; |
1453 | 485k | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
1454 | 485k | const char *encoding = NULL; |
1455 | 485k | const char *errors = NULL; |
1456 | | |
1457 | 485k | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
1458 | 485k | /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
1459 | 485k | if (!args) { |
1460 | 0 | goto exit; |
1461 | 0 | } |
1462 | 485k | if (!noptargs) { |
1463 | 0 | goto skip_optional_pos; |
1464 | 0 | } |
1465 | 485k | if (args[0]) { |
1466 | 485k | if (!PyUnicode_Check(args[0])) { |
1467 | 0 | _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]); |
1468 | 0 | goto exit; |
1469 | 0 | } |
1470 | 485k | Py_ssize_t encoding_length; |
1471 | 485k | encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length); |
1472 | 485k | if (encoding == NULL) { |
1473 | 0 | goto exit; |
1474 | 0 | } |
1475 | 485k | if (strlen(encoding) != (size_t)encoding_length) { |
1476 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
1477 | 0 | goto exit; |
1478 | 0 | } |
1479 | 485k | if (!--noptargs) { |
1480 | 0 | goto skip_optional_pos; |
1481 | 0 | } |
1482 | 485k | } |
1483 | 485k | if (!PyUnicode_Check(args[1])) { |
1484 | 0 | _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]); |
1485 | 0 | goto exit; |
1486 | 0 | } |
1487 | 485k | Py_ssize_t errors_length; |
1488 | 485k | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
1489 | 485k | if (errors == NULL) { |
1490 | 0 | goto exit; |
1491 | 0 | } |
1492 | 485k | if (strlen(errors) != (size_t)errors_length) { |
1493 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
1494 | 0 | goto exit; |
1495 | 0 | } |
1496 | 485k | skip_optional_pos: |
1497 | 485k | Py_BEGIN_CRITICAL_SECTION(self); |
1498 | 485k | return_value = bytearray_decode_impl((PyByteArrayObject *)self, encoding, errors); |
1499 | 485k | Py_END_CRITICAL_SECTION(); |
1500 | | |
1501 | 485k | exit: |
1502 | 485k | return return_value; |
1503 | 485k | } |
1504 | | |
1505 | | PyDoc_STRVAR(bytearray_join__doc__, |
1506 | | "join($self, iterable_of_bytes, /)\n" |
1507 | | "--\n" |
1508 | | "\n" |
1509 | | "Concatenate any number of bytes/bytearray objects.\n" |
1510 | | "\n" |
1511 | | "The bytearray whose method is called is inserted in between each pair.\n" |
1512 | | "\n" |
1513 | | "The result is returned as a new bytearray object."); |
1514 | | |
1515 | | #define BYTEARRAY_JOIN_METHODDEF \ |
1516 | | {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__}, |
1517 | | |
1518 | | static PyObject * |
1519 | | bytearray_join_impl(PyByteArrayObject *self, PyObject *iterable_of_bytes); |
1520 | | |
1521 | | static PyObject * |
1522 | | bytearray_join(PyObject *self, PyObject *iterable_of_bytes) |
1523 | 0 | { |
1524 | 0 | PyObject *return_value = NULL; |
1525 | |
|
1526 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1527 | 0 | return_value = bytearray_join_impl((PyByteArrayObject *)self, iterable_of_bytes); |
1528 | 0 | Py_END_CRITICAL_SECTION(); |
1529 | |
|
1530 | 0 | return return_value; |
1531 | 0 | } |
1532 | | |
1533 | | PyDoc_STRVAR(bytearray_splitlines__doc__, |
1534 | | "splitlines($self, /, keepends=False)\n" |
1535 | | "--\n" |
1536 | | "\n" |
1537 | | "Return a list of the lines in the bytearray, breaking at line boundaries.\n" |
1538 | | "\n" |
1539 | | "Line breaks are not included in the resulting list unless keepends is given and\n" |
1540 | | "true."); |
1541 | | |
1542 | | #define BYTEARRAY_SPLITLINES_METHODDEF \ |
1543 | | {"splitlines", _PyCFunction_CAST(bytearray_splitlines), METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__}, |
1544 | | |
1545 | | static PyObject * |
1546 | | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends); |
1547 | | |
1548 | | static PyObject * |
1549 | | bytearray_splitlines(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
1550 | 0 | { |
1551 | 0 | PyObject *return_value = NULL; |
1552 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
1553 | |
|
1554 | 0 | #define NUM_KEYWORDS 1 |
1555 | 0 | static struct { |
1556 | 0 | PyGC_Head _this_is_not_used; |
1557 | 0 | PyObject_VAR_HEAD |
1558 | 0 | Py_hash_t ob_hash; |
1559 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
1560 | 0 | } _kwtuple = { |
1561 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
1562 | 0 | .ob_hash = -1, |
1563 | 0 | .ob_item = { &_Py_ID(keepends), }, |
1564 | 0 | }; |
1565 | 0 | #undef NUM_KEYWORDS |
1566 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
1567 | |
|
1568 | | #else // !Py_BUILD_CORE |
1569 | | # define KWTUPLE NULL |
1570 | | #endif // !Py_BUILD_CORE |
1571 | |
|
1572 | 0 | static const char * const _keywords[] = {"keepends", NULL}; |
1573 | 0 | static _PyArg_Parser _parser = { |
1574 | 0 | .keywords = _keywords, |
1575 | 0 | .fname = "splitlines", |
1576 | 0 | .kwtuple = KWTUPLE, |
1577 | 0 | }; |
1578 | 0 | #undef KWTUPLE |
1579 | 0 | PyObject *argsbuf[1]; |
1580 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
1581 | 0 | int keepends = 0; |
1582 | |
|
1583 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
1584 | 0 | /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
1585 | 0 | if (!args) { |
1586 | 0 | goto exit; |
1587 | 0 | } |
1588 | 0 | if (!noptargs) { |
1589 | 0 | goto skip_optional_pos; |
1590 | 0 | } |
1591 | 0 | keepends = PyObject_IsTrue(args[0]); |
1592 | 0 | if (keepends < 0) { |
1593 | 0 | goto exit; |
1594 | 0 | } |
1595 | 0 | skip_optional_pos: |
1596 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1597 | 0 | return_value = bytearray_splitlines_impl((PyByteArrayObject *)self, keepends); |
1598 | 0 | Py_END_CRITICAL_SECTION(); |
1599 | |
|
1600 | 0 | exit: |
1601 | 0 | return return_value; |
1602 | 0 | } |
1603 | | |
1604 | | PyDoc_STRVAR(bytearray_fromhex__doc__, |
1605 | | "fromhex($type, string, /)\n" |
1606 | | "--\n" |
1607 | | "\n" |
1608 | | "Create a bytearray object from a string of hexadecimal numbers.\n" |
1609 | | "\n" |
1610 | | "Spaces between two numbers are accepted.\n" |
1611 | | "Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')"); |
1612 | | |
1613 | | #define BYTEARRAY_FROMHEX_METHODDEF \ |
1614 | | {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__}, |
1615 | | |
1616 | | static PyObject * |
1617 | | bytearray_fromhex_impl(PyTypeObject *type, PyObject *string); |
1618 | | |
1619 | | static PyObject * |
1620 | | bytearray_fromhex(PyObject *type, PyObject *string) |
1621 | 0 | { |
1622 | 0 | PyObject *return_value = NULL; |
1623 | |
|
1624 | 0 | return_value = bytearray_fromhex_impl((PyTypeObject *)type, string); |
1625 | |
|
1626 | 0 | return return_value; |
1627 | 0 | } |
1628 | | |
1629 | | PyDoc_STRVAR(bytearray_hex__doc__, |
1630 | | "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n" |
1631 | | "--\n" |
1632 | | "\n" |
1633 | | "Create a string of hexadecimal numbers from a bytearray object.\n" |
1634 | | "\n" |
1635 | | " sep\n" |
1636 | | " An optional single character or byte to separate hex bytes.\n" |
1637 | | " bytes_per_sep\n" |
1638 | | " How many bytes between separators. Positive values count from the\n" |
1639 | | " right, negative values count from the left.\n" |
1640 | | "\n" |
1641 | | "Example:\n" |
1642 | | ">>> value = bytearray([0xb9, 0x01, 0xef])\n" |
1643 | | ">>> value.hex()\n" |
1644 | | "\'b901ef\'\n" |
1645 | | ">>> value.hex(\':\')\n" |
1646 | | "\'b9:01:ef\'\n" |
1647 | | ">>> value.hex(\':\', 2)\n" |
1648 | | "\'b9:01ef\'\n" |
1649 | | ">>> value.hex(\':\', -2)\n" |
1650 | | "\'b901:ef\'"); |
1651 | | |
1652 | | #define BYTEARRAY_HEX_METHODDEF \ |
1653 | | {"hex", _PyCFunction_CAST(bytearray_hex), METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__}, |
1654 | | |
1655 | | static PyObject * |
1656 | | bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep); |
1657 | | |
1658 | | static PyObject * |
1659 | | bytearray_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
1660 | 0 | { |
1661 | 0 | PyObject *return_value = NULL; |
1662 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
1663 | |
|
1664 | 0 | #define NUM_KEYWORDS 2 |
1665 | 0 | static struct { |
1666 | 0 | PyGC_Head _this_is_not_used; |
1667 | 0 | PyObject_VAR_HEAD |
1668 | 0 | Py_hash_t ob_hash; |
1669 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
1670 | 0 | } _kwtuple = { |
1671 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
1672 | 0 | .ob_hash = -1, |
1673 | 0 | .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), }, |
1674 | 0 | }; |
1675 | 0 | #undef NUM_KEYWORDS |
1676 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
1677 | |
|
1678 | | #else // !Py_BUILD_CORE |
1679 | | # define KWTUPLE NULL |
1680 | | #endif // !Py_BUILD_CORE |
1681 | |
|
1682 | 0 | static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL}; |
1683 | 0 | static _PyArg_Parser _parser = { |
1684 | 0 | .keywords = _keywords, |
1685 | 0 | .fname = "hex", |
1686 | 0 | .kwtuple = KWTUPLE, |
1687 | 0 | }; |
1688 | 0 | #undef KWTUPLE |
1689 | 0 | PyObject *argsbuf[2]; |
1690 | 0 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
1691 | 0 | PyObject *sep = NULL; |
1692 | 0 | int bytes_per_sep = 1; |
1693 | |
|
1694 | 0 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
1695 | 0 | /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
1696 | 0 | if (!args) { |
1697 | 0 | goto exit; |
1698 | 0 | } |
1699 | 0 | if (!noptargs) { |
1700 | 0 | goto skip_optional_pos; |
1701 | 0 | } |
1702 | 0 | if (args[0]) { |
1703 | 0 | sep = args[0]; |
1704 | 0 | if (!--noptargs) { |
1705 | 0 | goto skip_optional_pos; |
1706 | 0 | } |
1707 | 0 | } |
1708 | 0 | bytes_per_sep = PyLong_AsInt(args[1]); |
1709 | 0 | if (bytes_per_sep == -1 && PyErr_Occurred()) { |
1710 | 0 | goto exit; |
1711 | 0 | } |
1712 | 0 | skip_optional_pos: |
1713 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1714 | 0 | return_value = bytearray_hex_impl((PyByteArrayObject *)self, sep, bytes_per_sep); |
1715 | 0 | Py_END_CRITICAL_SECTION(); |
1716 | |
|
1717 | 0 | exit: |
1718 | 0 | return return_value; |
1719 | 0 | } |
1720 | | |
1721 | | PyDoc_STRVAR(bytearray_reduce__doc__, |
1722 | | "__reduce__($self, /)\n" |
1723 | | "--\n" |
1724 | | "\n" |
1725 | | "Return state information for pickling."); |
1726 | | |
1727 | | #define BYTEARRAY_REDUCE_METHODDEF \ |
1728 | | {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__}, |
1729 | | |
1730 | | static PyObject * |
1731 | | bytearray_reduce_impl(PyByteArrayObject *self); |
1732 | | |
1733 | | static PyObject * |
1734 | | bytearray_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) |
1735 | 0 | { |
1736 | 0 | PyObject *return_value = NULL; |
1737 | |
|
1738 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1739 | 0 | return_value = bytearray_reduce_impl((PyByteArrayObject *)self); |
1740 | 0 | Py_END_CRITICAL_SECTION(); |
1741 | |
|
1742 | 0 | return return_value; |
1743 | 0 | } |
1744 | | |
1745 | | PyDoc_STRVAR(bytearray_reduce_ex__doc__, |
1746 | | "__reduce_ex__($self, proto=0, /)\n" |
1747 | | "--\n" |
1748 | | "\n" |
1749 | | "Return state information for pickling."); |
1750 | | |
1751 | | #define BYTEARRAY_REDUCE_EX_METHODDEF \ |
1752 | | {"__reduce_ex__", _PyCFunction_CAST(bytearray_reduce_ex), METH_FASTCALL, bytearray_reduce_ex__doc__}, |
1753 | | |
1754 | | static PyObject * |
1755 | | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto); |
1756 | | |
1757 | | static PyObject * |
1758 | | bytearray_reduce_ex(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
1759 | 0 | { |
1760 | 0 | PyObject *return_value = NULL; |
1761 | 0 | int proto = 0; |
1762 | |
|
1763 | 0 | if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) { |
1764 | 0 | goto exit; |
1765 | 0 | } |
1766 | 0 | if (nargs < 1) { |
1767 | 0 | goto skip_optional; |
1768 | 0 | } |
1769 | 0 | proto = PyLong_AsInt(args[0]); |
1770 | 0 | if (proto == -1 && PyErr_Occurred()) { |
1771 | 0 | goto exit; |
1772 | 0 | } |
1773 | 0 | skip_optional: |
1774 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1775 | 0 | return_value = bytearray_reduce_ex_impl((PyByteArrayObject *)self, proto); |
1776 | 0 | Py_END_CRITICAL_SECTION(); |
1777 | |
|
1778 | 0 | exit: |
1779 | 0 | return return_value; |
1780 | 0 | } |
1781 | | |
1782 | | PyDoc_STRVAR(bytearray_sizeof__doc__, |
1783 | | "__sizeof__($self, /)\n" |
1784 | | "--\n" |
1785 | | "\n" |
1786 | | "Returns the size of the bytearray object in memory, in bytes."); |
1787 | | |
1788 | | #define BYTEARRAY_SIZEOF_METHODDEF \ |
1789 | | {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__}, |
1790 | | |
1791 | | static PyObject * |
1792 | | bytearray_sizeof_impl(PyByteArrayObject *self); |
1793 | | |
1794 | | static PyObject * |
1795 | | bytearray_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored)) |
1796 | 0 | { |
1797 | 0 | return bytearray_sizeof_impl((PyByteArrayObject *)self); |
1798 | 0 | } |
1799 | | /*[clinic end generated code: output=be6d28193bc96a2c input=a9049054013a1b77]*/ |