ยปCore Development>Code coverage>Objects/bytearrayobject.c

Python code coverage for Objects/bytearrayobject.c

#countcontent
1n/a/* PyByteArray (bytearray) implementation */
2n/a
3n/a#define PY_SSIZE_T_CLEAN
4n/a#include "Python.h"
5n/a#include "structmember.h"
6n/a#include "bytes_methods.h"
7n/a#include "bytesobject.h"
8n/a#include "pystrhex.h"
9n/a
10n/a/*[clinic input]
11n/aclass bytearray "PyByteArrayObject *" "&PyByteArray_Type"
12n/a[clinic start generated code]*/
13n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
14n/a
15n/achar _PyByteArray_empty_string[] = "";
16n/a
17n/avoid
18n/aPyByteArray_Fini(void)
19n/a{
20n/a}
21n/a
22n/aint
23n/aPyByteArray_Init(void)
24n/a{
25n/a return 1;
26n/a}
27n/a
28n/a/* end nullbytes support */
29n/a
30n/a/* Helpers */
31n/a
32n/astatic int
33n/a_getbytevalue(PyObject* arg, int *value)
34n/a{
35n/a long face_value;
36n/a
37n/a if (PyLong_Check(arg)) {
38n/a face_value = PyLong_AsLong(arg);
39n/a } else {
40n/a PyObject *index = PyNumber_Index(arg);
41n/a if (index == NULL) {
42n/a PyErr_Format(PyExc_TypeError, "an integer is required");
43n/a *value = -1;
44n/a return 0;
45n/a }
46n/a face_value = PyLong_AsLong(index);
47n/a Py_DECREF(index);
48n/a }
49n/a
50n/a if (face_value < 0 || face_value >= 256) {
51n/a /* this includes the OverflowError in case the long is too large */
52n/a PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
53n/a *value = -1;
54n/a return 0;
55n/a }
56n/a
57n/a *value = face_value;
58n/a return 1;
59n/a}
60n/a
61n/astatic int
62n/abytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
63n/a{
64n/a void *ptr;
65n/a if (view == NULL) {
66n/a PyErr_SetString(PyExc_BufferError,
67n/a "bytearray_getbuffer: view==NULL argument is obsolete");
68n/a return -1;
69n/a }
70n/a ptr = (void *) PyByteArray_AS_STRING(obj);
71n/a /* cannot fail if view != NULL and readonly == 0 */
72n/a (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
73n/a obj->ob_exports++;
74n/a return 0;
75n/a}
76n/a
77n/astatic void
78n/abytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
79n/a{
80n/a obj->ob_exports--;
81n/a}
82n/a
83n/astatic int
84n/a_canresize(PyByteArrayObject *self)
85n/a{
86n/a if (self->ob_exports > 0) {
87n/a PyErr_SetString(PyExc_BufferError,
88n/a "Existing exports of data: object cannot be re-sized");
89n/a return 0;
90n/a }
91n/a return 1;
92n/a}
93n/a
94n/a#include "clinic/bytearrayobject.c.h"
95n/a
96n/a/* Direct API functions */
97n/a
98n/aPyObject *
99n/aPyByteArray_FromObject(PyObject *input)
100n/a{
101n/a return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
102n/a input, NULL);
103n/a}
104n/a
105n/aPyObject *
106n/aPyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
107n/a{
108n/a PyByteArrayObject *new;
109n/a Py_ssize_t alloc;
110n/a
111n/a if (size < 0) {
112n/a PyErr_SetString(PyExc_SystemError,
113n/a "Negative size passed to PyByteArray_FromStringAndSize");
114n/a return NULL;
115n/a }
116n/a
117n/a /* Prevent buffer overflow when setting alloc to size+1. */
118n/a if (size == PY_SSIZE_T_MAX) {
119n/a return PyErr_NoMemory();
120n/a }
121n/a
122n/a new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
123n/a if (new == NULL)
124n/a return NULL;
125n/a
126n/a if (size == 0) {
127n/a new->ob_bytes = NULL;
128n/a alloc = 0;
129n/a }
130n/a else {
131n/a alloc = size + 1;
132n/a new->ob_bytes = PyObject_Malloc(alloc);
133n/a if (new->ob_bytes == NULL) {
134n/a Py_DECREF(new);
135n/a return PyErr_NoMemory();
136n/a }
137n/a if (bytes != NULL && size > 0)
138n/a memcpy(new->ob_bytes, bytes, size);
139n/a new->ob_bytes[size] = '\0'; /* Trailing null byte */
140n/a }
141n/a Py_SIZE(new) = size;
142n/a new->ob_alloc = alloc;
143n/a new->ob_start = new->ob_bytes;
144n/a new->ob_exports = 0;
145n/a
146n/a return (PyObject *)new;
147n/a}
148n/a
149n/aPy_ssize_t
150n/aPyByteArray_Size(PyObject *self)
151n/a{
152n/a assert(self != NULL);
153n/a assert(PyByteArray_Check(self));
154n/a
155n/a return PyByteArray_GET_SIZE(self);
156n/a}
157n/a
158n/achar *
159n/aPyByteArray_AsString(PyObject *self)
160n/a{
161n/a assert(self != NULL);
162n/a assert(PyByteArray_Check(self));
163n/a
164n/a return PyByteArray_AS_STRING(self);
165n/a}
166n/a
167n/aint
168n/aPyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
169n/a{
170n/a void *sval;
171n/a PyByteArrayObject *obj = ((PyByteArrayObject *)self);
172n/a /* All computations are done unsigned to avoid integer overflows
173n/a (see issue #22335). */
174n/a size_t alloc = (size_t) obj->ob_alloc;
175n/a size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
176n/a size_t size = (size_t) requested_size;
177n/a
178n/a assert(self != NULL);
179n/a assert(PyByteArray_Check(self));
180n/a assert(logical_offset <= alloc);
181n/a assert(requested_size >= 0);
182n/a
183n/a if (requested_size == Py_SIZE(self)) {
184n/a return 0;
185n/a }
186n/a if (!_canresize(obj)) {
187n/a return -1;
188n/a }
189n/a
190n/a if (size + logical_offset + 1 <= alloc) {
191n/a /* Current buffer is large enough to host the requested size,
192n/a decide on a strategy. */
193n/a if (size < alloc / 2) {
194n/a /* Major downsize; resize down to exact size */
195n/a alloc = size + 1;
196n/a }
197n/a else {
198n/a /* Minor downsize; quick exit */
199n/a Py_SIZE(self) = size;
200n/a PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
201n/a return 0;
202n/a }
203n/a }
204n/a else {
205n/a /* Need growing, decide on a strategy */
206n/a if (size <= alloc * 1.125) {
207n/a /* Moderate upsize; overallocate similar to list_resize() */
208n/a alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
209n/a }
210n/a else {
211n/a /* Major upsize; resize up to exact size */
212n/a alloc = size + 1;
213n/a }
214n/a }
215n/a if (alloc > PY_SSIZE_T_MAX) {
216n/a PyErr_NoMemory();
217n/a return -1;
218n/a }
219n/a
220n/a if (logical_offset > 0) {
221n/a sval = PyObject_Malloc(alloc);
222n/a if (sval == NULL) {
223n/a PyErr_NoMemory();
224n/a return -1;
225n/a }
226n/a memcpy(sval, PyByteArray_AS_STRING(self),
227n/a Py_MIN(requested_size, Py_SIZE(self)));
228n/a PyObject_Free(obj->ob_bytes);
229n/a }
230n/a else {
231n/a sval = PyObject_Realloc(obj->ob_bytes, alloc);
232n/a if (sval == NULL) {
233n/a PyErr_NoMemory();
234n/a return -1;
235n/a }
236n/a }
237n/a
238n/a obj->ob_bytes = obj->ob_start = sval;
239n/a Py_SIZE(self) = size;
240n/a obj->ob_alloc = alloc;
241n/a obj->ob_bytes[size] = '\0'; /* Trailing null byte */
242n/a
243n/a return 0;
244n/a}
245n/a
246n/aPyObject *
247n/aPyByteArray_Concat(PyObject *a, PyObject *b)
248n/a{
249n/a Py_buffer va, vb;
250n/a PyByteArrayObject *result = NULL;
251n/a
252n/a va.len = -1;
253n/a vb.len = -1;
254n/a if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
255n/a PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
256n/a PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
257n/a Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
258n/a goto done;
259n/a }
260n/a
261n/a if (va.len > PY_SSIZE_T_MAX - vb.len) {
262n/a PyErr_NoMemory();
263n/a goto done;
264n/a }
265n/a
266n/a result = (PyByteArrayObject *) \
267n/a PyByteArray_FromStringAndSize(NULL, va.len + vb.len);
268n/a if (result != NULL) {
269n/a memcpy(result->ob_bytes, va.buf, va.len);
270n/a memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
271n/a }
272n/a
273n/a done:
274n/a if (va.len != -1)
275n/a PyBuffer_Release(&va);
276n/a if (vb.len != -1)
277n/a PyBuffer_Release(&vb);
278n/a return (PyObject *)result;
279n/a}
280n/a
281n/a/* Functions stuffed into the type object */
282n/a
283n/astatic Py_ssize_t
284n/abytearray_length(PyByteArrayObject *self)
285n/a{
286n/a return Py_SIZE(self);
287n/a}
288n/a
289n/astatic PyObject *
290n/abytearray_iconcat(PyByteArrayObject *self, PyObject *other)
291n/a{
292n/a Py_ssize_t size;
293n/a Py_buffer vo;
294n/a
295n/a if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
296n/a PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
297n/a Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
298n/a return NULL;
299n/a }
300n/a
301n/a size = Py_SIZE(self);
302n/a if (size > PY_SSIZE_T_MAX - vo.len) {
303n/a PyBuffer_Release(&vo);
304n/a return PyErr_NoMemory();
305n/a }
306n/a if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
307n/a PyBuffer_Release(&vo);
308n/a return NULL;
309n/a }
310n/a memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
311n/a PyBuffer_Release(&vo);
312n/a Py_INCREF(self);
313n/a return (PyObject *)self;
314n/a}
315n/a
316n/astatic PyObject *
317n/abytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
318n/a{
319n/a PyByteArrayObject *result;
320n/a Py_ssize_t mysize;
321n/a Py_ssize_t size;
322n/a
323n/a if (count < 0)
324n/a count = 0;
325n/a mysize = Py_SIZE(self);
326n/a if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
327n/a return PyErr_NoMemory();
328n/a size = mysize * count;
329n/a result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
330n/a if (result != NULL && size != 0) {
331n/a if (mysize == 1)
332n/a memset(result->ob_bytes, self->ob_bytes[0], size);
333n/a else {
334n/a Py_ssize_t i;
335n/a for (i = 0; i < count; i++)
336n/a memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
337n/a }
338n/a }
339n/a return (PyObject *)result;
340n/a}
341n/a
342n/astatic PyObject *
343n/abytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
344n/a{
345n/a Py_ssize_t mysize;
346n/a Py_ssize_t size;
347n/a char *buf;
348n/a
349n/a if (count < 0)
350n/a count = 0;
351n/a mysize = Py_SIZE(self);
352n/a if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
353n/a return PyErr_NoMemory();
354n/a size = mysize * count;
355n/a if (PyByteArray_Resize((PyObject *)self, size) < 0)
356n/a return NULL;
357n/a
358n/a buf = PyByteArray_AS_STRING(self);
359n/a if (mysize == 1)
360n/a memset(buf, buf[0], size);
361n/a else {
362n/a Py_ssize_t i;
363n/a for (i = 1; i < count; i++)
364n/a memcpy(buf + i*mysize, buf, mysize);
365n/a }
366n/a
367n/a Py_INCREF(self);
368n/a return (PyObject *)self;
369n/a}
370n/a
371n/astatic PyObject *
372n/abytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
373n/a{
374n/a if (i < 0)
375n/a i += Py_SIZE(self);
376n/a if (i < 0 || i >= Py_SIZE(self)) {
377n/a PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
378n/a return NULL;
379n/a }
380n/a return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
381n/a}
382n/a
383n/astatic PyObject *
384n/abytearray_subscript(PyByteArrayObject *self, PyObject *index)
385n/a{
386n/a if (PyIndex_Check(index)) {
387n/a Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
388n/a
389n/a if (i == -1 && PyErr_Occurred())
390n/a return NULL;
391n/a
392n/a if (i < 0)
393n/a i += PyByteArray_GET_SIZE(self);
394n/a
395n/a if (i < 0 || i >= Py_SIZE(self)) {
396n/a PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
397n/a return NULL;
398n/a }
399n/a return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
400n/a }
401n/a else if (PySlice_Check(index)) {
402n/a Py_ssize_t start, stop, step, slicelength, cur, i;
403n/a if (PySlice_GetIndicesEx(index,
404n/a PyByteArray_GET_SIZE(self),
405n/a &start, &stop, &step, &slicelength) < 0) {
406n/a return NULL;
407n/a }
408n/a
409n/a if (slicelength <= 0)
410n/a return PyByteArray_FromStringAndSize("", 0);
411n/a else if (step == 1) {
412n/a return PyByteArray_FromStringAndSize(
413n/a PyByteArray_AS_STRING(self) + start, slicelength);
414n/a }
415n/a else {
416n/a char *source_buf = PyByteArray_AS_STRING(self);
417n/a char *result_buf;
418n/a PyObject *result;
419n/a
420n/a result = PyByteArray_FromStringAndSize(NULL, slicelength);
421n/a if (result == NULL)
422n/a return NULL;
423n/a
424n/a result_buf = PyByteArray_AS_STRING(result);
425n/a for (cur = start, i = 0; i < slicelength;
426n/a cur += step, i++) {
427n/a result_buf[i] = source_buf[cur];
428n/a }
429n/a return result;
430n/a }
431n/a }
432n/a else {
433n/a PyErr_Format(PyExc_TypeError,
434n/a "bytearray indices must be integers or slices, not %.200s",
435n/a Py_TYPE(index)->tp_name);
436n/a return NULL;
437n/a }
438n/a}
439n/a
440n/astatic int
441n/abytearray_setslice_linear(PyByteArrayObject *self,
442n/a Py_ssize_t lo, Py_ssize_t hi,
443n/a char *bytes, Py_ssize_t bytes_len)
444n/a{
445n/a Py_ssize_t avail = hi - lo;
446n/a char *buf = PyByteArray_AS_STRING(self);
447n/a Py_ssize_t growth = bytes_len - avail;
448n/a int res = 0;
449n/a assert(avail >= 0);
450n/a
451n/a if (growth < 0) {
452n/a if (!_canresize(self))
453n/a return -1;
454n/a
455n/a if (lo == 0) {
456n/a /* Shrink the buffer by advancing its logical start */
457n/a self->ob_start -= growth;
458n/a /*
459n/a 0 lo hi old_size
460n/a | |<----avail----->|<-----tail------>|
461n/a | |<-bytes_len->|<-----tail------>|
462n/a 0 new_lo new_hi new_size
463n/a */
464n/a }
465n/a else {
466n/a /*
467n/a 0 lo hi old_size
468n/a | |<----avail----->|<-----tomove------>|
469n/a | |<-bytes_len->|<-----tomove------>|
470n/a 0 lo new_hi new_size
471n/a */
472n/a memmove(buf + lo + bytes_len, buf + hi,
473n/a Py_SIZE(self) - hi);
474n/a }
475n/a if (PyByteArray_Resize((PyObject *)self,
476n/a Py_SIZE(self) + growth) < 0) {
477n/a /* Issue #19578: Handling the memory allocation failure here is
478n/a tricky here because the bytearray object has already been
479n/a modified. Depending on growth and lo, the behaviour is
480n/a different.
481n/a
482n/a If growth < 0 and lo != 0, the operation is completed, but a
483n/a MemoryError is still raised and the memory block is not
484n/a shrunk. Otherwise, the bytearray is restored in its previous
485n/a state and a MemoryError is raised. */
486n/a if (lo == 0) {
487n/a self->ob_start += growth;
488n/a return -1;
489n/a }
490n/a /* memmove() removed bytes, the bytearray object cannot be
491n/a restored in its previous state. */
492n/a Py_SIZE(self) += growth;
493n/a res = -1;
494n/a }
495n/a buf = PyByteArray_AS_STRING(self);
496n/a }
497n/a else if (growth > 0) {
498n/a if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
499n/a PyErr_NoMemory();
500n/a return -1;
501n/a }
502n/a
503n/a if (PyByteArray_Resize((PyObject *)self,
504n/a Py_SIZE(self) + growth) < 0) {
505n/a return -1;
506n/a }
507n/a buf = PyByteArray_AS_STRING(self);
508n/a /* Make the place for the additional bytes */
509n/a /*
510n/a 0 lo hi old_size
511n/a | |<-avail->|<-----tomove------>|
512n/a | |<---bytes_len-->|<-----tomove------>|
513n/a 0 lo new_hi new_size
514n/a */
515n/a memmove(buf + lo + bytes_len, buf + hi,
516n/a Py_SIZE(self) - lo - bytes_len);
517n/a }
518n/a
519n/a if (bytes_len > 0)
520n/a memcpy(buf + lo, bytes, bytes_len);
521n/a return res;
522n/a}
523n/a
524n/astatic int
525n/abytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
526n/a PyObject *values)
527n/a{
528n/a Py_ssize_t needed;
529n/a void *bytes;
530n/a Py_buffer vbytes;
531n/a int res = 0;
532n/a
533n/a vbytes.len = -1;
534n/a if (values == (PyObject *)self) {
535n/a /* Make a copy and call this function recursively */
536n/a int err;
537n/a values = PyByteArray_FromObject(values);
538n/a if (values == NULL)
539n/a return -1;
540n/a err = bytearray_setslice(self, lo, hi, values);
541n/a Py_DECREF(values);
542n/a return err;
543n/a }
544n/a if (values == NULL) {
545n/a /* del b[lo:hi] */
546n/a bytes = NULL;
547n/a needed = 0;
548n/a }
549n/a else {
550n/a if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
551n/a PyErr_Format(PyExc_TypeError,
552n/a "can't set bytearray slice from %.100s",
553n/a Py_TYPE(values)->tp_name);
554n/a return -1;
555n/a }
556n/a needed = vbytes.len;
557n/a bytes = vbytes.buf;
558n/a }
559n/a
560n/a if (lo < 0)
561n/a lo = 0;
562n/a if (hi < lo)
563n/a hi = lo;
564n/a if (hi > Py_SIZE(self))
565n/a hi = Py_SIZE(self);
566n/a
567n/a res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
568n/a if (vbytes.len != -1)
569n/a PyBuffer_Release(&vbytes);
570n/a return res;
571n/a}
572n/a
573n/astatic int
574n/abytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
575n/a{
576n/a int ival;
577n/a
578n/a if (i < 0)
579n/a i += Py_SIZE(self);
580n/a
581n/a if (i < 0 || i >= Py_SIZE(self)) {
582n/a PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
583n/a return -1;
584n/a }
585n/a
586n/a if (value == NULL)
587n/a return bytearray_setslice(self, i, i+1, NULL);
588n/a
589n/a if (!_getbytevalue(value, &ival))
590n/a return -1;
591n/a
592n/a PyByteArray_AS_STRING(self)[i] = ival;
593n/a return 0;
594n/a}
595n/a
596n/astatic int
597n/abytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
598n/a{
599n/a Py_ssize_t start, stop, step, slicelen, needed;
600n/a char *buf, *bytes;
601n/a buf = PyByteArray_AS_STRING(self);
602n/a
603n/a if (PyIndex_Check(index)) {
604n/a Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
605n/a
606n/a if (i == -1 && PyErr_Occurred())
607n/a return -1;
608n/a
609n/a if (i < 0)
610n/a i += PyByteArray_GET_SIZE(self);
611n/a
612n/a if (i < 0 || i >= Py_SIZE(self)) {
613n/a PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
614n/a return -1;
615n/a }
616n/a
617n/a if (values == NULL) {
618n/a /* Fall through to slice assignment */
619n/a start = i;
620n/a stop = i + 1;
621n/a step = 1;
622n/a slicelen = 1;
623n/a }
624n/a else {
625n/a int ival;
626n/a if (!_getbytevalue(values, &ival))
627n/a return -1;
628n/a buf[i] = (char)ival;
629n/a return 0;
630n/a }
631n/a }
632n/a else if (PySlice_Check(index)) {
633n/a if (PySlice_GetIndicesEx(index,
634n/a PyByteArray_GET_SIZE(self),
635n/a &start, &stop, &step, &slicelen) < 0) {
636n/a return -1;
637n/a }
638n/a }
639n/a else {
640n/a PyErr_Format(PyExc_TypeError,
641n/a "bytearray indices must be integers or slices, not %.200s",
642n/a Py_TYPE(index)->tp_name);
643n/a return -1;
644n/a }
645n/a
646n/a if (values == NULL) {
647n/a bytes = NULL;
648n/a needed = 0;
649n/a }
650n/a else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
651n/a int err;
652n/a if (PyNumber_Check(values) || PyUnicode_Check(values)) {
653n/a PyErr_SetString(PyExc_TypeError,
654n/a "can assign only bytes, buffers, or iterables "
655n/a "of ints in range(0, 256)");
656n/a return -1;
657n/a }
658n/a /* Make a copy and call this function recursively */
659n/a values = PyByteArray_FromObject(values);
660n/a if (values == NULL)
661n/a return -1;
662n/a err = bytearray_ass_subscript(self, index, values);
663n/a Py_DECREF(values);
664n/a return err;
665n/a }
666n/a else {
667n/a assert(PyByteArray_Check(values));
668n/a bytes = PyByteArray_AS_STRING(values);
669n/a needed = Py_SIZE(values);
670n/a }
671n/a /* Make sure b[5:2] = ... inserts before 5, not before 2. */
672n/a if ((step < 0 && start < stop) ||
673n/a (step > 0 && start > stop))
674n/a stop = start;
675n/a if (step == 1) {
676n/a return bytearray_setslice_linear(self, start, stop, bytes, needed);
677n/a }
678n/a else {
679n/a if (needed == 0) {
680n/a /* Delete slice */
681n/a size_t cur;
682n/a Py_ssize_t i;
683n/a
684n/a if (!_canresize(self))
685n/a return -1;
686n/a
687n/a if (slicelen == 0)
688n/a /* Nothing to do here. */
689n/a return 0;
690n/a
691n/a if (step < 0) {
692n/a stop = start + 1;
693n/a start = stop + step * (slicelen - 1) - 1;
694n/a step = -step;
695n/a }
696n/a for (cur = start, i = 0;
697n/a i < slicelen; cur += step, i++) {
698n/a Py_ssize_t lim = step - 1;
699n/a
700n/a if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
701n/a lim = PyByteArray_GET_SIZE(self) - cur - 1;
702n/a
703n/a memmove(buf + cur - i,
704n/a buf + cur + 1, lim);
705n/a }
706n/a /* Move the tail of the bytes, in one chunk */
707n/a cur = start + (size_t)slicelen*step;
708n/a if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
709n/a memmove(buf + cur - slicelen,
710n/a buf + cur,
711n/a PyByteArray_GET_SIZE(self) - cur);
712n/a }
713n/a if (PyByteArray_Resize((PyObject *)self,
714n/a PyByteArray_GET_SIZE(self) - slicelen) < 0)
715n/a return -1;
716n/a
717n/a return 0;
718n/a }
719n/a else {
720n/a /* Assign slice */
721n/a Py_ssize_t i;
722n/a size_t cur;
723n/a
724n/a if (needed != slicelen) {
725n/a PyErr_Format(PyExc_ValueError,
726n/a "attempt to assign bytes of size %zd "
727n/a "to extended slice of size %zd",
728n/a needed, slicelen);
729n/a return -1;
730n/a }
731n/a for (cur = start, i = 0; i < slicelen; cur += step, i++)
732n/a buf[cur] = bytes[i];
733n/a return 0;
734n/a }
735n/a }
736n/a}
737n/a
738n/astatic int
739n/abytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
740n/a{
741n/a static char *kwlist[] = {"source", "encoding", "errors", 0};
742n/a PyObject *arg = NULL;
743n/a const char *encoding = NULL;
744n/a const char *errors = NULL;
745n/a Py_ssize_t count;
746n/a PyObject *it;
747n/a PyObject *(*iternext)(PyObject *);
748n/a
749n/a if (Py_SIZE(self) != 0) {
750n/a /* Empty previous contents (yes, do this first of all!) */
751n/a if (PyByteArray_Resize((PyObject *)self, 0) < 0)
752n/a return -1;
753n/a }
754n/a
755n/a /* Parse arguments */
756n/a if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist,
757n/a &arg, &encoding, &errors))
758n/a return -1;
759n/a
760n/a /* Make a quick exit if no first argument */
761n/a if (arg == NULL) {
762n/a if (encoding != NULL || errors != NULL) {
763n/a PyErr_SetString(PyExc_TypeError,
764n/a "encoding or errors without sequence argument");
765n/a return -1;
766n/a }
767n/a return 0;
768n/a }
769n/a
770n/a if (PyUnicode_Check(arg)) {
771n/a /* Encode via the codec registry */
772n/a PyObject *encoded, *new;
773n/a if (encoding == NULL) {
774n/a PyErr_SetString(PyExc_TypeError,
775n/a "string argument without an encoding");
776n/a return -1;
777n/a }
778n/a encoded = PyUnicode_AsEncodedString(arg, encoding, errors);
779n/a if (encoded == NULL)
780n/a return -1;
781n/a assert(PyBytes_Check(encoded));
782n/a new = bytearray_iconcat(self, encoded);
783n/a Py_DECREF(encoded);
784n/a if (new == NULL)
785n/a return -1;
786n/a Py_DECREF(new);
787n/a return 0;
788n/a }
789n/a
790n/a /* If it's not unicode, there can't be encoding or errors */
791n/a if (encoding != NULL || errors != NULL) {
792n/a PyErr_SetString(PyExc_TypeError,
793n/a "encoding or errors without a string argument");
794n/a return -1;
795n/a }
796n/a
797n/a /* Is it an int? */
798n/a if (PyIndex_Check(arg)) {
799n/a count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
800n/a if (count == -1 && PyErr_Occurred()) {
801n/a if (PyErr_ExceptionMatches(PyExc_OverflowError))
802n/a return -1;
803n/a PyErr_Clear(); /* fall through */
804n/a }
805n/a else {
806n/a if (count < 0) {
807n/a PyErr_SetString(PyExc_ValueError, "negative count");
808n/a return -1;
809n/a }
810n/a if (count > 0) {
811n/a if (PyByteArray_Resize((PyObject *)self, count))
812n/a return -1;
813n/a memset(PyByteArray_AS_STRING(self), 0, count);
814n/a }
815n/a return 0;
816n/a }
817n/a }
818n/a
819n/a /* Use the buffer API */
820n/a if (PyObject_CheckBuffer(arg)) {
821n/a Py_ssize_t size;
822n/a Py_buffer view;
823n/a if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
824n/a return -1;
825n/a size = view.len;
826n/a if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
827n/a if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
828n/a &view, size, 'C') < 0)
829n/a goto fail;
830n/a PyBuffer_Release(&view);
831n/a return 0;
832n/a fail:
833n/a PyBuffer_Release(&view);
834n/a return -1;
835n/a }
836n/a
837n/a /* XXX Optimize this if the arguments is a list, tuple */
838n/a
839n/a /* Get the iterator */
840n/a it = PyObject_GetIter(arg);
841n/a if (it == NULL)
842n/a return -1;
843n/a iternext = *Py_TYPE(it)->tp_iternext;
844n/a
845n/a /* Run the iterator to exhaustion */
846n/a for (;;) {
847n/a PyObject *item;
848n/a int rc, value;
849n/a
850n/a /* Get the next item */
851n/a item = iternext(it);
852n/a if (item == NULL) {
853n/a if (PyErr_Occurred()) {
854n/a if (!PyErr_ExceptionMatches(PyExc_StopIteration))
855n/a goto error;
856n/a PyErr_Clear();
857n/a }
858n/a break;
859n/a }
860n/a
861n/a /* Interpret it as an int (__index__) */
862n/a rc = _getbytevalue(item, &value);
863n/a Py_DECREF(item);
864n/a if (!rc)
865n/a goto error;
866n/a
867n/a /* Append the byte */
868n/a if (Py_SIZE(self) + 1 < self->ob_alloc) {
869n/a Py_SIZE(self)++;
870n/a PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
871n/a }
872n/a else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
873n/a goto error;
874n/a PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
875n/a }
876n/a
877n/a /* Clean up and return success */
878n/a Py_DECREF(it);
879n/a return 0;
880n/a
881n/a error:
882n/a /* Error handling when it != NULL */
883n/a Py_DECREF(it);
884n/a return -1;
885n/a}
886n/a
887n/a/* Mostly copied from string_repr, but without the
888n/a "smart quote" functionality. */
889n/astatic PyObject *
890n/abytearray_repr(PyByteArrayObject *self)
891n/a{
892n/a const char *quote_prefix = "bytearray(b";
893n/a const char *quote_postfix = ")";
894n/a Py_ssize_t length = Py_SIZE(self);
895n/a /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
896n/a size_t newsize;
897n/a PyObject *v;
898n/a Py_ssize_t i;
899n/a char *bytes;
900n/a char c;
901n/a char *p;
902n/a int quote;
903n/a char *test, *start;
904n/a char *buffer;
905n/a
906n/a if (length > (PY_SSIZE_T_MAX - 15) / 4) {
907n/a PyErr_SetString(PyExc_OverflowError,
908n/a "bytearray object is too large to make repr");
909n/a return NULL;
910n/a }
911n/a
912n/a newsize = 15 + length * 4;
913n/a buffer = PyObject_Malloc(newsize);
914n/a if (buffer == NULL) {
915n/a PyErr_NoMemory();
916n/a return NULL;
917n/a }
918n/a
919n/a /* Figure out which quote to use; single is preferred */
920n/a quote = '\'';
921n/a start = PyByteArray_AS_STRING(self);
922n/a for (test = start; test < start+length; ++test) {
923n/a if (*test == '"') {
924n/a quote = '\''; /* back to single */
925n/a break;
926n/a }
927n/a else if (*test == '\'')
928n/a quote = '"';
929n/a }
930n/a
931n/a p = buffer;
932n/a while (*quote_prefix)
933n/a *p++ = *quote_prefix++;
934n/a *p++ = quote;
935n/a
936n/a bytes = PyByteArray_AS_STRING(self);
937n/a for (i = 0; i < length; i++) {
938n/a /* There's at least enough room for a hex escape
939n/a and a closing quote. */
940n/a assert(newsize - (p - buffer) >= 5);
941n/a c = bytes[i];
942n/a if (c == '\'' || c == '\\')
943n/a *p++ = '\\', *p++ = c;
944n/a else if (c == '\t')
945n/a *p++ = '\\', *p++ = 't';
946n/a else if (c == '\n')
947n/a *p++ = '\\', *p++ = 'n';
948n/a else if (c == '\r')
949n/a *p++ = '\\', *p++ = 'r';
950n/a else if (c == 0)
951n/a *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
952n/a else if (c < ' ' || c >= 0x7f) {
953n/a *p++ = '\\';
954n/a *p++ = 'x';
955n/a *p++ = Py_hexdigits[(c & 0xf0) >> 4];
956n/a *p++ = Py_hexdigits[c & 0xf];
957n/a }
958n/a else
959n/a *p++ = c;
960n/a }
961n/a assert(newsize - (p - buffer) >= 1);
962n/a *p++ = quote;
963n/a while (*quote_postfix) {
964n/a *p++ = *quote_postfix++;
965n/a }
966n/a
967n/a v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
968n/a PyObject_Free(buffer);
969n/a return v;
970n/a}
971n/a
972n/astatic PyObject *
973n/abytearray_str(PyObject *op)
974n/a{
975n/a if (Py_BytesWarningFlag) {
976n/a if (PyErr_WarnEx(PyExc_BytesWarning,
977n/a "str() on a bytearray instance", 1))
978n/a return NULL;
979n/a }
980n/a return bytearray_repr((PyByteArrayObject*)op);
981n/a}
982n/a
983n/astatic PyObject *
984n/abytearray_richcompare(PyObject *self, PyObject *other, int op)
985n/a{
986n/a Py_ssize_t self_size, other_size;
987n/a Py_buffer self_bytes, other_bytes;
988n/a PyObject *res;
989n/a Py_ssize_t minsize;
990n/a int cmp, rc;
991n/a
992n/a /* Bytes can be compared to anything that supports the (binary)
993n/a buffer API. Except that a comparison with Unicode is always an
994n/a error, even if the comparison is for equality. */
995n/a rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
996n/a if (!rc)
997n/a rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
998n/a if (rc < 0)
999n/a return NULL;
1000n/a if (rc) {
1001n/a if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
1002n/a if (PyErr_WarnEx(PyExc_BytesWarning,
1003n/a "Comparison between bytearray and string", 1))
1004n/a return NULL;
1005n/a }
1006n/a
1007n/a Py_RETURN_NOTIMPLEMENTED;
1008n/a }
1009n/a
1010n/a if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
1011n/a PyErr_Clear();
1012n/a Py_RETURN_NOTIMPLEMENTED;
1013n/a }
1014n/a self_size = self_bytes.len;
1015n/a
1016n/a if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
1017n/a PyErr_Clear();
1018n/a PyBuffer_Release(&self_bytes);
1019n/a Py_RETURN_NOTIMPLEMENTED;
1020n/a }
1021n/a other_size = other_bytes.len;
1022n/a
1023n/a if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
1024n/a /* Shortcut: if the lengths differ, the objects differ */
1025n/a cmp = (op == Py_NE);
1026n/a }
1027n/a else {
1028n/a minsize = self_size;
1029n/a if (other_size < minsize)
1030n/a minsize = other_size;
1031n/a
1032n/a cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
1033n/a /* In ISO C, memcmp() guarantees to use unsigned bytes! */
1034n/a
1035n/a if (cmp == 0) {
1036n/a if (self_size < other_size)
1037n/a cmp = -1;
1038n/a else if (self_size > other_size)
1039n/a cmp = 1;
1040n/a }
1041n/a
1042n/a switch (op) {
1043n/a case Py_LT: cmp = cmp < 0; break;
1044n/a case Py_LE: cmp = cmp <= 0; break;
1045n/a case Py_EQ: cmp = cmp == 0; break;
1046n/a case Py_NE: cmp = cmp != 0; break;
1047n/a case Py_GT: cmp = cmp > 0; break;
1048n/a case Py_GE: cmp = cmp >= 0; break;
1049n/a }
1050n/a }
1051n/a
1052n/a res = cmp ? Py_True : Py_False;
1053n/a PyBuffer_Release(&self_bytes);
1054n/a PyBuffer_Release(&other_bytes);
1055n/a Py_INCREF(res);
1056n/a return res;
1057n/a}
1058n/a
1059n/astatic void
1060n/abytearray_dealloc(PyByteArrayObject *self)
1061n/a{
1062n/a if (self->ob_exports > 0) {
1063n/a PyErr_SetString(PyExc_SystemError,
1064n/a "deallocated bytearray object has exported buffers");
1065n/a PyErr_Print();
1066n/a }
1067n/a if (self->ob_bytes != 0) {
1068n/a PyObject_Free(self->ob_bytes);
1069n/a }
1070n/a Py_TYPE(self)->tp_free((PyObject *)self);
1071n/a}
1072n/a
1073n/a
1074n/a/* -------------------------------------------------------------------- */
1075n/a/* Methods */
1076n/a
1077n/a#define FASTSEARCH fastsearch
1078n/a#define STRINGLIB(F) stringlib_##F
1079n/a#define STRINGLIB_CHAR char
1080n/a#define STRINGLIB_SIZEOF_CHAR 1
1081n/a#define STRINGLIB_LEN PyByteArray_GET_SIZE
1082n/a#define STRINGLIB_STR PyByteArray_AS_STRING
1083n/a#define STRINGLIB_NEW PyByteArray_FromStringAndSize
1084n/a#define STRINGLIB_ISSPACE Py_ISSPACE
1085n/a#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
1086n/a#define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1087n/a#define STRINGLIB_MUTABLE 1
1088n/a
1089n/a#include "stringlib/fastsearch.h"
1090n/a#include "stringlib/count.h"
1091n/a#include "stringlib/find.h"
1092n/a#include "stringlib/join.h"
1093n/a#include "stringlib/partition.h"
1094n/a#include "stringlib/split.h"
1095n/a#include "stringlib/ctype.h"
1096n/a#include "stringlib/transmogrify.h"
1097n/a
1098n/a
1099n/astatic PyObject *
1100n/abytearray_find(PyByteArrayObject *self, PyObject *args)
1101n/a{
1102n/a return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1103n/a}
1104n/a
1105n/astatic PyObject *
1106n/abytearray_count(PyByteArrayObject *self, PyObject *args)
1107n/a{
1108n/a return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1109n/a}
1110n/a
1111n/a/*[clinic input]
1112n/abytearray.clear
1113n/a
1114n/aRemove all items from the bytearray.
1115n/a[clinic start generated code]*/
1116n/a
1117n/astatic PyObject *
1118n/abytearray_clear_impl(PyByteArrayObject *self)
1119n/a/*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/
1120n/a{
1121n/a if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1122n/a return NULL;
1123n/a Py_RETURN_NONE;
1124n/a}
1125n/a
1126n/a/*[clinic input]
1127n/abytearray.copy
1128n/a
1129n/aReturn a copy of B.
1130n/a[clinic start generated code]*/
1131n/a
1132n/astatic PyObject *
1133n/abytearray_copy_impl(PyByteArrayObject *self)
1134n/a/*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/
1135n/a{
1136n/a return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self),
1137n/a PyByteArray_GET_SIZE(self));
1138n/a}
1139n/a
1140n/astatic PyObject *
1141n/abytearray_index(PyByteArrayObject *self, PyObject *args)
1142n/a{
1143n/a return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1144n/a}
1145n/a
1146n/astatic PyObject *
1147n/abytearray_rfind(PyByteArrayObject *self, PyObject *args)
1148n/a{
1149n/a return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1150n/a}
1151n/a
1152n/astatic PyObject *
1153n/abytearray_rindex(PyByteArrayObject *self, PyObject *args)
1154n/a{
1155n/a return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1156n/a}
1157n/a
1158n/astatic int
1159n/abytearray_contains(PyObject *self, PyObject *arg)
1160n/a{
1161n/a return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1162n/a}
1163n/a
1164n/astatic PyObject *
1165n/abytearray_startswith(PyByteArrayObject *self, PyObject *args)
1166n/a{
1167n/a return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1168n/a}
1169n/a
1170n/astatic PyObject *
1171n/abytearray_endswith(PyByteArrayObject *self, PyObject *args)
1172n/a{
1173n/a return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args);
1174n/a}
1175n/a
1176n/a
1177n/a/*[clinic input]
1178n/abytearray.translate
1179n/a
1180n/a table: object
1181n/a Translation table, which must be a bytes object of length 256.
1182n/a /
1183n/a delete as deletechars: object(c_default="NULL") = b''
1184n/a
1185n/aReturn a copy with each character mapped by the given translation table.
1186n/a
1187n/aAll characters occurring in the optional argument delete are removed.
1188n/aThe remaining characters are mapped through the given translation table.
1189n/a[clinic start generated code]*/
1190n/a
1191n/astatic PyObject *
1192n/abytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
1193n/a PyObject *deletechars)
1194n/a/*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/
1195n/a{
1196n/a char *input, *output;
1197n/a const char *table_chars;
1198n/a Py_ssize_t i, c;
1199n/a PyObject *input_obj = (PyObject*)self;
1200n/a const char *output_start;
1201n/a Py_ssize_t inlen;
1202n/a PyObject *result = NULL;
1203n/a int trans_table[256];
1204n/a Py_buffer vtable, vdel;
1205n/a
1206n/a if (table == Py_None) {
1207n/a table_chars = NULL;
1208n/a table = NULL;
1209n/a } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
1210n/a return NULL;
1211n/a } else {
1212n/a if (vtable.len != 256) {
1213n/a PyErr_SetString(PyExc_ValueError,
1214n/a "translation table must be 256 characters long");
1215n/a PyBuffer_Release(&vtable);
1216n/a return NULL;
1217n/a }
1218n/a table_chars = (const char*)vtable.buf;
1219n/a }
1220n/a
1221n/a if (deletechars != NULL) {
1222n/a if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
1223n/a if (table != NULL)
1224n/a PyBuffer_Release(&vtable);
1225n/a return NULL;
1226n/a }
1227n/a }
1228n/a else {
1229n/a vdel.buf = NULL;
1230n/a vdel.len = 0;
1231n/a }
1232n/a
1233n/a inlen = PyByteArray_GET_SIZE(input_obj);
1234n/a result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1235n/a if (result == NULL)
1236n/a goto done;
1237n/a output_start = output = PyByteArray_AS_STRING(result);
1238n/a input = PyByteArray_AS_STRING(input_obj);
1239n/a
1240n/a if (vdel.len == 0 && table_chars != NULL) {
1241n/a /* If no deletions are required, use faster code */
1242n/a for (i = inlen; --i >= 0; ) {
1243n/a c = Py_CHARMASK(*input++);
1244n/a *output++ = table_chars[c];
1245n/a }
1246n/a goto done;
1247n/a }
1248n/a
1249n/a if (table_chars == NULL) {
1250n/a for (i = 0; i < 256; i++)
1251n/a trans_table[i] = Py_CHARMASK(i);
1252n/a } else {
1253n/a for (i = 0; i < 256; i++)
1254n/a trans_table[i] = Py_CHARMASK(table_chars[i]);
1255n/a }
1256n/a
1257n/a for (i = 0; i < vdel.len; i++)
1258n/a trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1259n/a
1260n/a for (i = inlen; --i >= 0; ) {
1261n/a c = Py_CHARMASK(*input++);
1262n/a if (trans_table[c] != -1)
1263n/a *output++ = (char)trans_table[c];
1264n/a }
1265n/a /* Fix the size of the resulting string */
1266n/a if (inlen > 0)
1267n/a if (PyByteArray_Resize(result, output - output_start) < 0) {
1268n/a Py_CLEAR(result);
1269n/a goto done;
1270n/a }
1271n/a
1272n/adone:
1273n/a if (table != NULL)
1274n/a PyBuffer_Release(&vtable);
1275n/a if (deletechars != NULL)
1276n/a PyBuffer_Release(&vdel);
1277n/a return result;
1278n/a}
1279n/a
1280n/a
1281n/a/*[clinic input]
1282n/a
1283n/a@staticmethod
1284n/abytearray.maketrans
1285n/a
1286n/a frm: Py_buffer
1287n/a to: Py_buffer
1288n/a /
1289n/a
1290n/aReturn a translation table useable for the bytes or bytearray translate method.
1291n/a
1292n/aThe returned table will be one where each byte in frm is mapped to the byte at
1293n/athe same position in to.
1294n/a
1295n/aThe bytes objects frm and to must be of the same length.
1296n/a[clinic start generated code]*/
1297n/a
1298n/astatic PyObject *
1299n/abytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
1300n/a/*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
1301n/a{
1302n/a return _Py_bytes_maketrans(frm, to);
1303n/a}
1304n/a
1305n/a
1306n/a/*[clinic input]
1307n/abytearray.replace
1308n/a
1309n/a old: Py_buffer
1310n/a new: Py_buffer
1311n/a count: Py_ssize_t = -1
1312n/a Maximum number of occurrences to replace.
1313n/a -1 (the default value) means replace all occurrences.
1314n/a /
1315n/a
1316n/aReturn a copy with all occurrences of substring old replaced by new.
1317n/a
1318n/aIf the optional argument count is given, only the first count occurrences are
1319n/areplaced.
1320n/a[clinic start generated code]*/
1321n/a
1322n/astatic PyObject *
1323n/abytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1324n/a Py_buffer *new, Py_ssize_t count)
1325n/a/*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
1326n/a{
1327n/a return stringlib_replace((PyObject *)self,
1328n/a (const char *)old->buf, old->len,
1329n/a (const char *)new->buf, new->len, count);
1330n/a}
1331n/a
1332n/a/*[clinic input]
1333n/abytearray.split
1334n/a
1335n/a sep: object = None
1336n/a The delimiter according which to split the bytearray.
1337n/a None (the default value) means split on ASCII whitespace characters
1338n/a (space, tab, return, newline, formfeed, vertical tab).
1339n/a maxsplit: Py_ssize_t = -1
1340n/a Maximum number of splits to do.
1341n/a -1 (the default value) means no limit.
1342n/a
1343n/aReturn a list of the sections in the bytearray, using sep as the delimiter.
1344n/a[clinic start generated code]*/
1345n/a
1346n/astatic PyObject *
1347n/abytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
1348n/a Py_ssize_t maxsplit)
1349n/a/*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
1350n/a{
1351n/a Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
1352n/a const char *s = PyByteArray_AS_STRING(self), *sub;
1353n/a PyObject *list;
1354n/a Py_buffer vsub;
1355n/a
1356n/a if (maxsplit < 0)
1357n/a maxsplit = PY_SSIZE_T_MAX;
1358n/a
1359n/a if (sep == Py_None)
1360n/a return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
1361n/a
1362n/a if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
1363n/a return NULL;
1364n/a sub = vsub.buf;
1365n/a n = vsub.len;
1366n/a
1367n/a list = stringlib_split(
1368n/a (PyObject*) self, s, len, sub, n, maxsplit
1369n/a );
1370n/a PyBuffer_Release(&vsub);
1371n/a return list;
1372n/a}
1373n/a
1374n/a/*[clinic input]
1375n/abytearray.partition
1376n/a
1377n/a sep: object
1378n/a /
1379n/a
1380n/aPartition the bytearray into three parts using the given separator.
1381n/a
1382n/aThis will search for the separator sep in the bytearray. If the separator is
1383n/afound, returns a 3-tuple containing the part before the separator, the
1384n/aseparator itself, and the part after it.
1385n/a
1386n/aIf the separator is not found, returns a 3-tuple containing the original
1387n/abytearray object and two empty bytearray objects.
1388n/a[clinic start generated code]*/
1389n/a
1390n/astatic PyObject *
1391n/abytearray_partition(PyByteArrayObject *self, PyObject *sep)
1392n/a/*[clinic end generated code: output=45d2525ddd35f957 input=86f89223892b70b5]*/
1393n/a{
1394n/a PyObject *bytesep, *result;
1395n/a
1396n/a bytesep = PyByteArray_FromObject(sep);
1397n/a if (! bytesep)
1398n/a return NULL;
1399n/a
1400n/a result = stringlib_partition(
1401n/a (PyObject*) self,
1402n/a PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1403n/a bytesep,
1404n/a PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1405n/a );
1406n/a
1407n/a Py_DECREF(bytesep);
1408n/a return result;
1409n/a}
1410n/a
1411n/a/*[clinic input]
1412n/abytearray.rpartition
1413n/a
1414n/a sep: object
1415n/a /
1416n/a
1417n/aPartition the bytes into three parts using the given separator.
1418n/a
1419n/aThis will search for the separator sep in the bytearray, starting and the end.
1420n/aIf the separator is found, returns a 3-tuple containing the part before the
1421n/aseparator, the separator itself, and the part after it.
1422n/a
1423n/aIf the separator is not found, returns a 3-tuple containing two empty bytearray
1424n/aobjects and the original bytearray object.
1425n/a[clinic start generated code]*/
1426n/a
1427n/astatic PyObject *
1428n/abytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
1429n/a/*[clinic end generated code: output=440de3c9426115e8 input=5f4094f2de87c8f3]*/
1430n/a{
1431n/a PyObject *bytesep, *result;
1432n/a
1433n/a bytesep = PyByteArray_FromObject(sep);
1434n/a if (! bytesep)
1435n/a return NULL;
1436n/a
1437n/a result = stringlib_rpartition(
1438n/a (PyObject*) self,
1439n/a PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
1440n/a bytesep,
1441n/a PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
1442n/a );
1443n/a
1444n/a Py_DECREF(bytesep);
1445n/a return result;
1446n/a}
1447n/a
1448n/a/*[clinic input]
1449n/abytearray.rsplit = bytearray.split
1450n/a
1451n/aReturn a list of the sections in the bytearray, using sep as the delimiter.
1452n/a
1453n/aSplitting is done starting at the end of the bytearray and working to the front.
1454n/a[clinic start generated code]*/
1455n/a
1456n/astatic PyObject *
1457n/abytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
1458n/a Py_ssize_t maxsplit)
1459n/a/*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
1460n/a{
1461n/a Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
1462n/a const char *s = PyByteArray_AS_STRING(self), *sub;
1463n/a PyObject *list;
1464n/a Py_buffer vsub;
1465n/a
1466n/a if (maxsplit < 0)
1467n/a maxsplit = PY_SSIZE_T_MAX;
1468n/a
1469n/a if (sep == Py_None)
1470n/a return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
1471n/a
1472n/a if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
1473n/a return NULL;
1474n/a sub = vsub.buf;
1475n/a n = vsub.len;
1476n/a
1477n/a list = stringlib_rsplit(
1478n/a (PyObject*) self, s, len, sub, n, maxsplit
1479n/a );
1480n/a PyBuffer_Release(&vsub);
1481n/a return list;
1482n/a}
1483n/a
1484n/a/*[clinic input]
1485n/abytearray.reverse
1486n/a
1487n/aReverse the order of the values in B in place.
1488n/a[clinic start generated code]*/
1489n/a
1490n/astatic PyObject *
1491n/abytearray_reverse_impl(PyByteArrayObject *self)
1492n/a/*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/
1493n/a{
1494n/a char swap, *head, *tail;
1495n/a Py_ssize_t i, j, n = Py_SIZE(self);
1496n/a
1497n/a j = n / 2;
1498n/a head = PyByteArray_AS_STRING(self);
1499n/a tail = head + n - 1;
1500n/a for (i = 0; i < j; i++) {
1501n/a swap = *head;
1502n/a *head++ = *tail;
1503n/a *tail-- = swap;
1504n/a }
1505n/a
1506n/a Py_RETURN_NONE;
1507n/a}
1508n/a
1509n/a
1510n/a/*[python input]
1511n/aclass bytesvalue_converter(CConverter):
1512n/a type = 'int'
1513n/a converter = '_getbytevalue'
1514n/a[python start generated code]*/
1515n/a/*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
1516n/a
1517n/a
1518n/a/*[clinic input]
1519n/abytearray.insert
1520n/a
1521n/a index: Py_ssize_t
1522n/a The index where the value is to be inserted.
1523n/a item: bytesvalue
1524n/a The item to be inserted.
1525n/a /
1526n/a
1527n/aInsert a single item into the bytearray before the given index.
1528n/a[clinic start generated code]*/
1529n/a
1530n/astatic PyObject *
1531n/abytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
1532n/a/*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/
1533n/a{
1534n/a Py_ssize_t n = Py_SIZE(self);
1535n/a char *buf;
1536n/a
1537n/a if (n == PY_SSIZE_T_MAX) {
1538n/a PyErr_SetString(PyExc_OverflowError,
1539n/a "cannot add more objects to bytearray");
1540n/a return NULL;
1541n/a }
1542n/a if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1543n/a return NULL;
1544n/a buf = PyByteArray_AS_STRING(self);
1545n/a
1546n/a if (index < 0) {
1547n/a index += n;
1548n/a if (index < 0)
1549n/a index = 0;
1550n/a }
1551n/a if (index > n)
1552n/a index = n;
1553n/a memmove(buf + index + 1, buf + index, n - index);
1554n/a buf[index] = item;
1555n/a
1556n/a Py_RETURN_NONE;
1557n/a}
1558n/a
1559n/a/*[clinic input]
1560n/abytearray.append
1561n/a
1562n/a item: bytesvalue
1563n/a The item to be appended.
1564n/a /
1565n/a
1566n/aAppend a single item to the end of the bytearray.
1567n/a[clinic start generated code]*/
1568n/a
1569n/astatic PyObject *
1570n/abytearray_append_impl(PyByteArrayObject *self, int item)
1571n/a/*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/
1572n/a{
1573n/a Py_ssize_t n = Py_SIZE(self);
1574n/a
1575n/a if (n == PY_SSIZE_T_MAX) {
1576n/a PyErr_SetString(PyExc_OverflowError,
1577n/a "cannot add more objects to bytearray");
1578n/a return NULL;
1579n/a }
1580n/a if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
1581n/a return NULL;
1582n/a
1583n/a PyByteArray_AS_STRING(self)[n] = item;
1584n/a
1585n/a Py_RETURN_NONE;
1586n/a}
1587n/a
1588n/a/*[clinic input]
1589n/abytearray.extend
1590n/a
1591n/a iterable_of_ints: object
1592n/a The iterable of items to append.
1593n/a /
1594n/a
1595n/aAppend all the items from the iterator or sequence to the end of the bytearray.
1596n/a[clinic start generated code]*/
1597n/a
1598n/astatic PyObject *
1599n/abytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
1600n/a/*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/
1601n/a{
1602n/a PyObject *it, *item, *bytearray_obj;
1603n/a Py_ssize_t buf_size = 0, len = 0;
1604n/a int value;
1605n/a char *buf;
1606n/a
1607n/a /* bytearray_setslice code only accepts something supporting PEP 3118. */
1608n/a if (PyObject_CheckBuffer(iterable_of_ints)) {
1609n/a if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
1610n/a return NULL;
1611n/a
1612n/a Py_RETURN_NONE;
1613n/a }
1614n/a
1615n/a it = PyObject_GetIter(iterable_of_ints);
1616n/a if (it == NULL)
1617n/a return NULL;
1618n/a
1619n/a /* Try to determine the length of the argument. 32 is arbitrary. */
1620n/a buf_size = PyObject_LengthHint(iterable_of_ints, 32);
1621n/a if (buf_size == -1) {
1622n/a Py_DECREF(it);
1623n/a return NULL;
1624n/a }
1625n/a
1626n/a bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
1627n/a if (bytearray_obj == NULL) {
1628n/a Py_DECREF(it);
1629n/a return NULL;
1630n/a }
1631n/a buf = PyByteArray_AS_STRING(bytearray_obj);
1632n/a
1633n/a while ((item = PyIter_Next(it)) != NULL) {
1634n/a if (! _getbytevalue(item, &value)) {
1635n/a Py_DECREF(item);
1636n/a Py_DECREF(it);
1637n/a Py_DECREF(bytearray_obj);
1638n/a return NULL;
1639n/a }
1640n/a buf[len++] = value;
1641n/a Py_DECREF(item);
1642n/a
1643n/a if (len >= buf_size) {
1644n/a Py_ssize_t addition;
1645n/a if (len == PY_SSIZE_T_MAX) {
1646n/a Py_DECREF(it);
1647n/a Py_DECREF(bytearray_obj);
1648n/a return PyErr_NoMemory();
1649n/a }
1650n/a addition = len >> 1;
1651n/a if (addition > PY_SSIZE_T_MAX - len - 1)
1652n/a buf_size = PY_SSIZE_T_MAX;
1653n/a else
1654n/a buf_size = len + addition + 1;
1655n/a if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) {
1656n/a Py_DECREF(it);
1657n/a Py_DECREF(bytearray_obj);
1658n/a return NULL;
1659n/a }
1660n/a /* Recompute the `buf' pointer, since the resizing operation may
1661n/a have invalidated it. */
1662n/a buf = PyByteArray_AS_STRING(bytearray_obj);
1663n/a }
1664n/a }
1665n/a Py_DECREF(it);
1666n/a
1667n/a /* Resize down to exact size. */
1668n/a if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) {
1669n/a Py_DECREF(bytearray_obj);
1670n/a return NULL;
1671n/a }
1672n/a
1673n/a if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
1674n/a Py_DECREF(bytearray_obj);
1675n/a return NULL;
1676n/a }
1677n/a Py_DECREF(bytearray_obj);
1678n/a
1679n/a Py_RETURN_NONE;
1680n/a}
1681n/a
1682n/a/*[clinic input]
1683n/abytearray.pop
1684n/a
1685n/a index: Py_ssize_t = -1
1686n/a The index from where to remove the item.
1687n/a -1 (the default value) means remove the last item.
1688n/a /
1689n/a
1690n/aRemove and return a single item from B.
1691n/a
1692n/aIf no index argument is given, will pop the last item.
1693n/a[clinic start generated code]*/
1694n/a
1695n/astatic PyObject *
1696n/abytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
1697n/a/*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/
1698n/a{
1699n/a int value;
1700n/a Py_ssize_t n = Py_SIZE(self);
1701n/a char *buf;
1702n/a
1703n/a if (n == 0) {
1704n/a PyErr_SetString(PyExc_IndexError,
1705n/a "pop from empty bytearray");
1706n/a return NULL;
1707n/a }
1708n/a if (index < 0)
1709n/a index += Py_SIZE(self);
1710n/a if (index < 0 || index >= Py_SIZE(self)) {
1711n/a PyErr_SetString(PyExc_IndexError, "pop index out of range");
1712n/a return NULL;
1713n/a }
1714n/a if (!_canresize(self))
1715n/a return NULL;
1716n/a
1717n/a buf = PyByteArray_AS_STRING(self);
1718n/a value = buf[index];
1719n/a memmove(buf + index, buf + index + 1, n - index);
1720n/a if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1721n/a return NULL;
1722n/a
1723n/a return PyLong_FromLong((unsigned char)value);
1724n/a}
1725n/a
1726n/a/*[clinic input]
1727n/abytearray.remove
1728n/a
1729n/a value: bytesvalue
1730n/a The value to remove.
1731n/a /
1732n/a
1733n/aRemove the first occurrence of a value in the bytearray.
1734n/a[clinic start generated code]*/
1735n/a
1736n/astatic PyObject *
1737n/abytearray_remove_impl(PyByteArrayObject *self, int value)
1738n/a/*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/
1739n/a{
1740n/a Py_ssize_t where, n = Py_SIZE(self);
1741n/a char *buf = PyByteArray_AS_STRING(self);
1742n/a
1743n/a where = stringlib_find_char(buf, n, value);
1744n/a if (where < 0) {
1745n/a PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
1746n/a return NULL;
1747n/a }
1748n/a if (!_canresize(self))
1749n/a return NULL;
1750n/a
1751n/a memmove(buf + where, buf + where + 1, n - where);
1752n/a if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
1753n/a return NULL;
1754n/a
1755n/a Py_RETURN_NONE;
1756n/a}
1757n/a
1758n/a/* XXX These two helpers could be optimized if argsize == 1 */
1759n/a
1760n/astatic Py_ssize_t
1761n/alstrip_helper(const char *myptr, Py_ssize_t mysize,
1762n/a const void *argptr, Py_ssize_t argsize)
1763n/a{
1764n/a Py_ssize_t i = 0;
1765n/a while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
1766n/a i++;
1767n/a return i;
1768n/a}
1769n/a
1770n/astatic Py_ssize_t
1771n/arstrip_helper(const char *myptr, Py_ssize_t mysize,
1772n/a const void *argptr, Py_ssize_t argsize)
1773n/a{
1774n/a Py_ssize_t i = mysize - 1;
1775n/a while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
1776n/a i--;
1777n/a return i + 1;
1778n/a}
1779n/a
1780n/a/*[clinic input]
1781n/abytearray.strip
1782n/a
1783n/a bytes: object = None
1784n/a /
1785n/a
1786n/aStrip leading and trailing bytes contained in the argument.
1787n/a
1788n/aIf the argument is omitted or None, strip leading and trailing ASCII whitespace.
1789n/a[clinic start generated code]*/
1790n/a
1791n/astatic PyObject *
1792n/abytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
1793n/a/*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
1794n/a{
1795n/a Py_ssize_t left, right, mysize, byteslen;
1796n/a char *myptr, *bytesptr;
1797n/a Py_buffer vbytes;
1798n/a
1799n/a if (bytes == Py_None) {
1800n/a bytesptr = "\t\n\r\f\v ";
1801n/a byteslen = 6;
1802n/a }
1803n/a else {
1804n/a if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
1805n/a return NULL;
1806n/a bytesptr = (char *) vbytes.buf;
1807n/a byteslen = vbytes.len;
1808n/a }
1809n/a myptr = PyByteArray_AS_STRING(self);
1810n/a mysize = Py_SIZE(self);
1811n/a left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
1812n/a if (left == mysize)
1813n/a right = left;
1814n/a else
1815n/a right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1816n/a if (bytes != Py_None)
1817n/a PyBuffer_Release(&vbytes);
1818n/a return PyByteArray_FromStringAndSize(myptr + left, right - left);
1819n/a}
1820n/a
1821n/a/*[clinic input]
1822n/abytearray.lstrip
1823n/a
1824n/a bytes: object = None
1825n/a /
1826n/a
1827n/aStrip leading bytes contained in the argument.
1828n/a
1829n/aIf the argument is omitted or None, strip leading ASCII whitespace.
1830n/a[clinic start generated code]*/
1831n/a
1832n/astatic PyObject *
1833n/abytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
1834n/a/*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
1835n/a{
1836n/a Py_ssize_t left, right, mysize, byteslen;
1837n/a char *myptr, *bytesptr;
1838n/a Py_buffer vbytes;
1839n/a
1840n/a if (bytes == Py_None) {
1841n/a bytesptr = "\t\n\r\f\v ";
1842n/a byteslen = 6;
1843n/a }
1844n/a else {
1845n/a if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
1846n/a return NULL;
1847n/a bytesptr = (char *) vbytes.buf;
1848n/a byteslen = vbytes.len;
1849n/a }
1850n/a myptr = PyByteArray_AS_STRING(self);
1851n/a mysize = Py_SIZE(self);
1852n/a left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
1853n/a right = mysize;
1854n/a if (bytes != Py_None)
1855n/a PyBuffer_Release(&vbytes);
1856n/a return PyByteArray_FromStringAndSize(myptr + left, right - left);
1857n/a}
1858n/a
1859n/a/*[clinic input]
1860n/abytearray.rstrip
1861n/a
1862n/a bytes: object = None
1863n/a /
1864n/a
1865n/aStrip trailing bytes contained in the argument.
1866n/a
1867n/aIf the argument is omitted or None, strip trailing ASCII whitespace.
1868n/a[clinic start generated code]*/
1869n/a
1870n/astatic PyObject *
1871n/abytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
1872n/a/*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
1873n/a{
1874n/a Py_ssize_t right, mysize, byteslen;
1875n/a char *myptr, *bytesptr;
1876n/a Py_buffer vbytes;
1877n/a
1878n/a if (bytes == Py_None) {
1879n/a bytesptr = "\t\n\r\f\v ";
1880n/a byteslen = 6;
1881n/a }
1882n/a else {
1883n/a if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
1884n/a return NULL;
1885n/a bytesptr = (char *) vbytes.buf;
1886n/a byteslen = vbytes.len;
1887n/a }
1888n/a myptr = PyByteArray_AS_STRING(self);
1889n/a mysize = Py_SIZE(self);
1890n/a right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
1891n/a if (bytes != Py_None)
1892n/a PyBuffer_Release(&vbytes);
1893n/a return PyByteArray_FromStringAndSize(myptr, right);
1894n/a}
1895n/a
1896n/a/*[clinic input]
1897n/abytearray.decode
1898n/a
1899n/a encoding: str(c_default="NULL") = 'utf-8'
1900n/a The encoding with which to decode the bytearray.
1901n/a errors: str(c_default="NULL") = 'strict'
1902n/a The error handling scheme to use for the handling of decoding errors.
1903n/a The default is 'strict' meaning that decoding errors raise a
1904n/a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
1905n/a as well as any other name registered with codecs.register_error that
1906n/a can handle UnicodeDecodeErrors.
1907n/a
1908n/aDecode the bytearray using the codec registered for encoding.
1909n/a[clinic start generated code]*/
1910n/a
1911n/astatic PyObject *
1912n/abytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
1913n/a const char *errors)
1914n/a/*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
1915n/a{
1916n/a if (encoding == NULL)
1917n/a encoding = PyUnicode_GetDefaultEncoding();
1918n/a return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
1919n/a}
1920n/a
1921n/aPyDoc_STRVAR(alloc_doc,
1922n/a"B.__alloc__() -> int\n\
1923n/a\n\
1924n/aReturn the number of bytes actually allocated.");
1925n/a
1926n/astatic PyObject *
1927n/abytearray_alloc(PyByteArrayObject *self)
1928n/a{
1929n/a return PyLong_FromSsize_t(self->ob_alloc);
1930n/a}
1931n/a
1932n/a/*[clinic input]
1933n/abytearray.join
1934n/a
1935n/a iterable_of_bytes: object
1936n/a /
1937n/a
1938n/aConcatenate any number of bytes/bytearray objects.
1939n/a
1940n/aThe bytearray whose method is called is inserted in between each pair.
1941n/a
1942n/aThe result is returned as a new bytearray object.
1943n/a[clinic start generated code]*/
1944n/a
1945n/astatic PyObject *
1946n/abytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
1947n/a/*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
1948n/a{
1949n/a return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
1950n/a}
1951n/a
1952n/a/*[clinic input]
1953n/abytearray.splitlines
1954n/a
1955n/a keepends: int(c_default="0") = False
1956n/a
1957n/aReturn a list of the lines in the bytearray, breaking at line boundaries.
1958n/a
1959n/aLine breaks are not included in the resulting list unless keepends is given and
1960n/atrue.
1961n/a[clinic start generated code]*/
1962n/a
1963n/astatic PyObject *
1964n/abytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
1965n/a/*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/
1966n/a{
1967n/a return stringlib_splitlines(
1968n/a (PyObject*) self, PyByteArray_AS_STRING(self),
1969n/a PyByteArray_GET_SIZE(self), keepends
1970n/a );
1971n/a}
1972n/a
1973n/a/*[clinic input]
1974n/a@classmethod
1975n/abytearray.fromhex
1976n/a
1977n/a string: unicode
1978n/a /
1979n/a
1980n/aCreate a bytearray object from a string of hexadecimal numbers.
1981n/a
1982n/aSpaces between two numbers are accepted.
1983n/aExample: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
1984n/a[clinic start generated code]*/
1985n/a
1986n/astatic PyObject *
1987n/abytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
1988n/a/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
1989n/a{
1990n/a PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
1991n/a if (type != &PyByteArray_Type && result != NULL) {
1992n/a Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
1993n/a result, NULL));
1994n/a }
1995n/a return result;
1996n/a}
1997n/a
1998n/aPyDoc_STRVAR(hex__doc__,
1999n/a"B.hex() -> string\n\
2000n/a\n\
2001n/aCreate a string of hexadecimal numbers from a bytearray object.\n\
2002n/aExample: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2003n/a
2004n/astatic PyObject *
2005n/abytearray_hex(PyBytesObject *self)
2006n/a{
2007n/a char* argbuf = PyByteArray_AS_STRING(self);
2008n/a Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2009n/a return _Py_strhex(argbuf, arglen);
2010n/a}
2011n/a
2012n/astatic PyObject *
2013n/a_common_reduce(PyByteArrayObject *self, int proto)
2014n/a{
2015n/a PyObject *dict;
2016n/a _Py_IDENTIFIER(__dict__);
2017n/a char *buf;
2018n/a
2019n/a dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
2020n/a if (dict == NULL) {
2021n/a PyErr_Clear();
2022n/a dict = Py_None;
2023n/a Py_INCREF(dict);
2024n/a }
2025n/a
2026n/a buf = PyByteArray_AS_STRING(self);
2027n/a if (proto < 3) {
2028n/a /* use str based reduction for backwards compatibility with Python 2.x */
2029n/a PyObject *latin1;
2030n/a if (Py_SIZE(self))
2031n/a latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
2032n/a else
2033n/a latin1 = PyUnicode_FromString("");
2034n/a return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
2035n/a }
2036n/a else {
2037n/a /* use more efficient byte based reduction */
2038n/a if (Py_SIZE(self)) {
2039n/a return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
2040n/a }
2041n/a else {
2042n/a return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2043n/a }
2044n/a }
2045n/a}
2046n/a
2047n/a/*[clinic input]
2048n/abytearray.__reduce__ as bytearray_reduce
2049n/a
2050n/aReturn state information for pickling.
2051n/a[clinic start generated code]*/
2052n/a
2053n/astatic PyObject *
2054n/abytearray_reduce_impl(PyByteArrayObject *self)
2055n/a/*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/
2056n/a{
2057n/a return _common_reduce(self, 2);
2058n/a}
2059n/a
2060n/a/*[clinic input]
2061n/abytearray.__reduce_ex__ as bytearray_reduce_ex
2062n/a
2063n/a proto: int = 0
2064n/a /
2065n/a
2066n/aReturn state information for pickling.
2067n/a[clinic start generated code]*/
2068n/a
2069n/astatic PyObject *
2070n/abytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
2071n/a/*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/
2072n/a{
2073n/a return _common_reduce(self, proto);
2074n/a}
2075n/a
2076n/a/*[clinic input]
2077n/abytearray.__sizeof__ as bytearray_sizeof
2078n/a
2079n/aReturns the size of the bytearray object in memory, in bytes.
2080n/a[clinic start generated code]*/
2081n/a
2082n/astatic PyObject *
2083n/abytearray_sizeof_impl(PyByteArrayObject *self)
2084n/a/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
2085n/a{
2086n/a Py_ssize_t res;
2087n/a
2088n/a res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char);
2089n/a return PyLong_FromSsize_t(res);
2090n/a}
2091n/a
2092n/astatic PySequenceMethods bytearray_as_sequence = {
2093n/a (lenfunc)bytearray_length, /* sq_length */
2094n/a (binaryfunc)PyByteArray_Concat, /* sq_concat */
2095n/a (ssizeargfunc)bytearray_repeat, /* sq_repeat */
2096n/a (ssizeargfunc)bytearray_getitem, /* sq_item */
2097n/a 0, /* sq_slice */
2098n/a (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2099n/a 0, /* sq_ass_slice */
2100n/a (objobjproc)bytearray_contains, /* sq_contains */
2101n/a (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2102n/a (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
2103n/a};
2104n/a
2105n/astatic PyMappingMethods bytearray_as_mapping = {
2106n/a (lenfunc)bytearray_length,
2107n/a (binaryfunc)bytearray_subscript,
2108n/a (objobjargproc)bytearray_ass_subscript,
2109n/a};
2110n/a
2111n/astatic PyBufferProcs bytearray_as_buffer = {
2112n/a (getbufferproc)bytearray_getbuffer,
2113n/a (releasebufferproc)bytearray_releasebuffer,
2114n/a};
2115n/a
2116n/astatic PyMethodDef
2117n/abytearray_methods[] = {
2118n/a {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
2119n/a BYTEARRAY_REDUCE_METHODDEF
2120n/a BYTEARRAY_REDUCE_EX_METHODDEF
2121n/a BYTEARRAY_SIZEOF_METHODDEF
2122n/a BYTEARRAY_APPEND_METHODDEF
2123n/a {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2124n/a _Py_capitalize__doc__},
2125n/a {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__},
2126n/a BYTEARRAY_CLEAR_METHODDEF
2127n/a BYTEARRAY_COPY_METHODDEF
2128n/a {"count", (PyCFunction)bytearray_count, METH_VARARGS,
2129n/a _Py_count__doc__},
2130n/a BYTEARRAY_DECODE_METHODDEF
2131n/a {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS,
2132n/a _Py_endswith__doc__},
2133n/a {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS,
2134n/a _Py_expandtabs__doc__},
2135n/a BYTEARRAY_EXTEND_METHODDEF
2136n/a {"find", (PyCFunction)bytearray_find, METH_VARARGS,
2137n/a _Py_find__doc__},
2138n/a BYTEARRAY_FROMHEX_METHODDEF
2139n/a {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2140n/a {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__},
2141n/a BYTEARRAY_INSERT_METHODDEF
2142n/a {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2143n/a _Py_isalnum__doc__},
2144n/a {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2145n/a _Py_isalpha__doc__},
2146n/a {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2147n/a _Py_isdigit__doc__},
2148n/a {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2149n/a _Py_islower__doc__},
2150n/a {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2151n/a _Py_isspace__doc__},
2152n/a {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2153n/a _Py_istitle__doc__},
2154n/a {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2155n/a _Py_isupper__doc__},
2156n/a BYTEARRAY_JOIN_METHODDEF
2157n/a {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__},
2158n/a {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
2159n/a BYTEARRAY_LSTRIP_METHODDEF
2160n/a BYTEARRAY_MAKETRANS_METHODDEF
2161n/a BYTEARRAY_PARTITION_METHODDEF
2162n/a BYTEARRAY_POP_METHODDEF
2163n/a BYTEARRAY_REMOVE_METHODDEF
2164n/a BYTEARRAY_REPLACE_METHODDEF
2165n/a BYTEARRAY_REVERSE_METHODDEF
2166n/a {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__},
2167n/a {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__},
2168n/a {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__},
2169n/a BYTEARRAY_RPARTITION_METHODDEF
2170n/a BYTEARRAY_RSPLIT_METHODDEF
2171n/a BYTEARRAY_RSTRIP_METHODDEF
2172n/a BYTEARRAY_SPLIT_METHODDEF
2173n/a BYTEARRAY_SPLITLINES_METHODDEF
2174n/a {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
2175n/a _Py_startswith__doc__},
2176n/a BYTEARRAY_STRIP_METHODDEF
2177n/a {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2178n/a _Py_swapcase__doc__},
2179n/a {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
2180n/a BYTEARRAY_TRANSLATE_METHODDEF
2181n/a {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2182n/a {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__},
2183n/a {NULL}
2184n/a};
2185n/a
2186n/astatic PyObject *
2187n/abytearray_mod(PyObject *v, PyObject *w)
2188n/a{
2189n/a if (!PyByteArray_Check(v))
2190n/a Py_RETURN_NOTIMPLEMENTED;
2191n/a return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);
2192n/a}
2193n/a
2194n/astatic PyNumberMethods bytearray_as_number = {
2195n/a 0, /*nb_add*/
2196n/a 0, /*nb_subtract*/
2197n/a 0, /*nb_multiply*/
2198n/a bytearray_mod, /*nb_remainder*/
2199n/a};
2200n/a
2201n/aPyDoc_STRVAR(bytearray_doc,
2202n/a"bytearray(iterable_of_ints) -> bytearray\n\
2203n/abytearray(string, encoding[, errors]) -> bytearray\n\
2204n/abytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2205n/abytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\
2206n/abytearray() -> empty bytes array\n\
2207n/a\n\
2208n/aConstruct a mutable bytearray object from:\n\
2209n/a - an iterable yielding integers in range(256)\n\
2210n/a - a text string encoded using the specified encoding\n\
2211n/a - a bytes or a buffer object\n\
2212n/a - any object implementing the buffer API.\n\
2213n/a - an integer");
2214n/a
2215n/a
2216n/astatic PyObject *bytearray_iter(PyObject *seq);
2217n/a
2218n/aPyTypeObject PyByteArray_Type = {
2219n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
2220n/a "bytearray",
2221n/a sizeof(PyByteArrayObject),
2222n/a 0,
2223n/a (destructor)bytearray_dealloc, /* tp_dealloc */
2224n/a 0, /* tp_print */
2225n/a 0, /* tp_getattr */
2226n/a 0, /* tp_setattr */
2227n/a 0, /* tp_reserved */
2228n/a (reprfunc)bytearray_repr, /* tp_repr */
2229n/a &bytearray_as_number, /* tp_as_number */
2230n/a &bytearray_as_sequence, /* tp_as_sequence */
2231n/a &bytearray_as_mapping, /* tp_as_mapping */
2232n/a 0, /* tp_hash */
2233n/a 0, /* tp_call */
2234n/a bytearray_str, /* tp_str */
2235n/a PyObject_GenericGetAttr, /* tp_getattro */
2236n/a 0, /* tp_setattro */
2237n/a &bytearray_as_buffer, /* tp_as_buffer */
2238n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2239n/a bytearray_doc, /* tp_doc */
2240n/a 0, /* tp_traverse */
2241n/a 0, /* tp_clear */
2242n/a (richcmpfunc)bytearray_richcompare, /* tp_richcompare */
2243n/a 0, /* tp_weaklistoffset */
2244n/a bytearray_iter, /* tp_iter */
2245n/a 0, /* tp_iternext */
2246n/a bytearray_methods, /* tp_methods */
2247n/a 0, /* tp_members */
2248n/a 0, /* tp_getset */
2249n/a 0, /* tp_base */
2250n/a 0, /* tp_dict */
2251n/a 0, /* tp_descr_get */
2252n/a 0, /* tp_descr_set */
2253n/a 0, /* tp_dictoffset */
2254n/a (initproc)bytearray_init, /* tp_init */
2255n/a PyType_GenericAlloc, /* tp_alloc */
2256n/a PyType_GenericNew, /* tp_new */
2257n/a PyObject_Del, /* tp_free */
2258n/a};
2259n/a
2260n/a/*********************** Bytes Iterator ****************************/
2261n/a
2262n/atypedef struct {
2263n/a PyObject_HEAD
2264n/a Py_ssize_t it_index;
2265n/a PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */
2266n/a} bytesiterobject;
2267n/a
2268n/astatic void
2269n/abytearrayiter_dealloc(bytesiterobject *it)
2270n/a{
2271n/a _PyObject_GC_UNTRACK(it);
2272n/a Py_XDECREF(it->it_seq);
2273n/a PyObject_GC_Del(it);
2274n/a}
2275n/a
2276n/astatic int
2277n/abytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg)
2278n/a{
2279n/a Py_VISIT(it->it_seq);
2280n/a return 0;
2281n/a}
2282n/a
2283n/astatic PyObject *
2284n/abytearrayiter_next(bytesiterobject *it)
2285n/a{
2286n/a PyByteArrayObject *seq;
2287n/a PyObject *item;
2288n/a
2289n/a assert(it != NULL);
2290n/a seq = it->it_seq;
2291n/a if (seq == NULL)
2292n/a return NULL;
2293n/a assert(PyByteArray_Check(seq));
2294n/a
2295n/a if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2296n/a item = PyLong_FromLong(
2297n/a (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
2298n/a if (item != NULL)
2299n/a ++it->it_index;
2300n/a return item;
2301n/a }
2302n/a
2303n/a it->it_seq = NULL;
2304n/a Py_DECREF(seq);
2305n/a return NULL;
2306n/a}
2307n/a
2308n/astatic PyObject *
2309n/abytearrayiter_length_hint(bytesiterobject *it)
2310n/a{
2311n/a Py_ssize_t len = 0;
2312n/a if (it->it_seq) {
2313n/a len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
2314n/a if (len < 0) {
2315n/a len = 0;
2316n/a }
2317n/a }
2318n/a return PyLong_FromSsize_t(len);
2319n/a}
2320n/a
2321n/aPyDoc_STRVAR(length_hint_doc,
2322n/a "Private method returning an estimate of len(list(it)).");
2323n/a
2324n/astatic PyObject *
2325n/abytearrayiter_reduce(bytesiterobject *it)
2326n/a{
2327n/a if (it->it_seq != NULL) {
2328n/a return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
2329n/a it->it_seq, it->it_index);
2330n/a } else {
2331n/a return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter"));
2332n/a }
2333n/a}
2334n/a
2335n/astatic PyObject *
2336n/abytearrayiter_setstate(bytesiterobject *it, PyObject *state)
2337n/a{
2338n/a Py_ssize_t index = PyLong_AsSsize_t(state);
2339n/a if (index == -1 && PyErr_Occurred())
2340n/a return NULL;
2341n/a if (it->it_seq != NULL) {
2342n/a if (index < 0)
2343n/a index = 0;
2344n/a else if (index > PyByteArray_GET_SIZE(it->it_seq))
2345n/a index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
2346n/a it->it_index = index;
2347n/a }
2348n/a Py_RETURN_NONE;
2349n/a}
2350n/a
2351n/aPyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
2352n/a
2353n/astatic PyMethodDef bytearrayiter_methods[] = {
2354n/a {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
2355n/a length_hint_doc},
2356n/a {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
2357n/a bytearray_reduce__doc__},
2358n/a {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
2359n/a setstate_doc},
2360n/a {NULL, NULL} /* sentinel */
2361n/a};
2362n/a
2363n/aPyTypeObject PyByteArrayIter_Type = {
2364n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
2365n/a "bytearray_iterator", /* tp_name */
2366n/a sizeof(bytesiterobject), /* tp_basicsize */
2367n/a 0, /* tp_itemsize */
2368n/a /* methods */
2369n/a (destructor)bytearrayiter_dealloc, /* tp_dealloc */
2370n/a 0, /* tp_print */
2371n/a 0, /* tp_getattr */
2372n/a 0, /* tp_setattr */
2373n/a 0, /* tp_reserved */
2374n/a 0, /* tp_repr */
2375n/a 0, /* tp_as_number */
2376n/a 0, /* tp_as_sequence */
2377n/a 0, /* tp_as_mapping */
2378n/a 0, /* tp_hash */
2379n/a 0, /* tp_call */
2380n/a 0, /* tp_str */
2381n/a PyObject_GenericGetAttr, /* tp_getattro */
2382n/a 0, /* tp_setattro */
2383n/a 0, /* tp_as_buffer */
2384n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2385n/a 0, /* tp_doc */
2386n/a (traverseproc)bytearrayiter_traverse, /* tp_traverse */
2387n/a 0, /* tp_clear */
2388n/a 0, /* tp_richcompare */
2389n/a 0, /* tp_weaklistoffset */
2390n/a PyObject_SelfIter, /* tp_iter */
2391n/a (iternextfunc)bytearrayiter_next, /* tp_iternext */
2392n/a bytearrayiter_methods, /* tp_methods */
2393n/a 0,
2394n/a};
2395n/a
2396n/astatic PyObject *
2397n/abytearray_iter(PyObject *seq)
2398n/a{
2399n/a bytesiterobject *it;
2400n/a
2401n/a if (!PyByteArray_Check(seq)) {
2402n/a PyErr_BadInternalCall();
2403n/a return NULL;
2404n/a }
2405n/a it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2406n/a if (it == NULL)
2407n/a return NULL;
2408n/a it->it_index = 0;
2409n/a Py_INCREF(seq);
2410n/a it->it_seq = (PyByteArrayObject *)seq;
2411n/a _PyObject_GC_TRACK(it);
2412n/a return (PyObject *)it;
2413n/a}