Coverage Report

Created: 2025-10-10 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/bytesobject.c
Line
Count
Source
1
/* bytes object implementation */
2
3
#include "Python.h"
4
#include "pycore_abstract.h"      // _PyIndex_Check()
5
#include "pycore_bytes_methods.h" // _Py_bytes_startswith()
6
#include "pycore_bytesobject.h"   // _PyBytes_Find(), _PyBytes_Repeat()
7
#include "pycore_call.h"          // _PyObject_CallNoArgs()
8
#include "pycore_ceval.h"         // _PyEval_GetBuiltin()
9
#include "pycore_format.h"        // F_LJUST
10
#include "pycore_freelist.h"      // _Py_FREELIST_FREE()
11
#include "pycore_global_objects.h"// _Py_GET_GLOBAL_OBJECT()
12
#include "pycore_initconfig.h"    // _PyStatus_OK()
13
#include "pycore_long.h"          // _PyLong_DigitValue
14
#include "pycore_object.h"        // _PyObject_GC_TRACK
15
#include "pycore_pymem.h"         // PYMEM_CLEANBYTE
16
#include "pycore_strhex.h"        // _Py_strhex_with_sep()
17
#include "pycore_unicodeobject.h" // _PyUnicode_FormatLong()
18
19
#include <stddef.h>
20
21
/*[clinic input]
22
class bytes "PyBytesObject *" "&PyBytes_Type"
23
[clinic start generated code]*/
24
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7a238f965d64892b]*/
25
26
#include "clinic/bytesobject.c.h"
27
28
/* PyBytesObject_SIZE gives the basic size of a bytes object; any memory allocation
29
   for a bytes object of length n should request PyBytesObject_SIZE + n bytes.
30
31
   Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves
32
   3 or 7 bytes per bytes object allocation on a typical system.
33
*/
34
38.6M
#define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1)
35
36
/* Forward declaration */
37
static void* _PyBytesWriter_ResizeAndUpdatePointer(PyBytesWriter *writer,
38
                                                   Py_ssize_t size, void *data);
39
static Py_ssize_t _PyBytesWriter_GetAllocated(PyBytesWriter *writer);
40
41
42
4.28M
#define CHARACTERS _Py_SINGLETON(bytes_characters)
43
#define CHARACTER(ch) \
44
4.28M
     ((PyBytesObject *)&(CHARACTERS[ch]));
45
6.27M
#define EMPTY (&_Py_SINGLETON(bytes_empty))
46
47
48
// Return a reference to the immortal empty bytes string singleton.
49
static inline PyObject* bytes_get_empty(void)
50
6.27M
{
51
6.27M
    PyObject *empty = &EMPTY->ob_base.ob_base;
52
6.27M
    assert(_Py_IsImmortal(empty));
53
6.27M
    return empty;
54
6.27M
}
55
56
57
static inline void
58
set_ob_shash(PyBytesObject *a, Py_hash_t hash)
59
19.6M
{
60
19.6M
_Py_COMP_DIAG_PUSH
61
19.6M
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
62
#ifdef Py_GIL_DISABLED
63
    _Py_atomic_store_ssize_relaxed(&a->ob_shash, hash);
64
#else
65
19.6M
    a->ob_shash = hash;
66
19.6M
#endif
67
19.6M
_Py_COMP_DIAG_POP
68
19.6M
}
69
70
static inline Py_hash_t
71
get_ob_shash(PyBytesObject *a)
72
4.44M
{
73
4.44M
_Py_COMP_DIAG_PUSH
74
4.44M
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
75
#ifdef Py_GIL_DISABLED
76
    return _Py_atomic_load_ssize_relaxed(&a->ob_shash);
77
#else
78
4.44M
    return a->ob_shash;
79
4.44M
#endif
80
4.44M
_Py_COMP_DIAG_POP
81
4.44M
}
82
83
84
/*
85
   For PyBytes_FromString(), the parameter 'str' points to a null-terminated
86
   string containing exactly 'size' bytes.
87
88
   For PyBytes_FromStringAndSize(), the parameter 'str' is
89
   either NULL or else points to a string containing at least 'size' bytes.
90
   For PyBytes_FromStringAndSize(), the string in the 'str' parameter does
91
   not have to be null-terminated.  (Therefore it is safe to construct a
92
   substring by calling 'PyBytes_FromStringAndSize(origstring, substrlen)'.)
93
   If 'str' is NULL then PyBytes_FromStringAndSize() will allocate 'size+1'
94
   bytes (setting the last byte to the null terminating character) and you can
95
   fill in the data yourself.  If 'str' is non-NULL then the resulting
96
   PyBytes object must be treated as immutable and you must not fill in nor
97
   alter the data yourself, since the strings may be shared.
98
99
   The PyObject member 'op->ob_size', which denotes the number of "extra
100
   items" in a variable-size object, will contain the number of bytes
101
   allocated for string data, not counting the null terminating character.
102
   It is therefore equal to the 'size' parameter (for
103
   PyBytes_FromStringAndSize()) or the length of the string in the 'str'
104
   parameter (for PyBytes_FromString()).
105
*/
106
static PyObject *
107
_PyBytes_FromSize(Py_ssize_t size, int use_calloc)
108
19.1M
{
109
19.1M
    PyBytesObject *op;
110
19.1M
    assert(size >= 0);
111
112
19.1M
    if (size == 0) {
113
0
        return bytes_get_empty();
114
0
    }
115
116
19.1M
    if ((size_t)size > (size_t)PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
117
0
        PyErr_SetString(PyExc_OverflowError,
118
0
                        "byte string is too large");
119
0
        return NULL;
120
0
    }
121
122
    /* Inline PyObject_NewVar */
123
19.1M
    if (use_calloc)
124
0
        op = (PyBytesObject *)PyObject_Calloc(1, PyBytesObject_SIZE + size);
125
19.1M
    else
126
19.1M
        op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size);
127
19.1M
    if (op == NULL) {
128
0
        return PyErr_NoMemory();
129
0
    }
130
19.1M
    _PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
131
19.1M
    set_ob_shash(op, -1);
132
19.1M
    if (!use_calloc) {
133
19.1M
        op->ob_sval[size] = '\0';
134
19.1M
    }
135
19.1M
    return (PyObject *) op;
136
19.1M
}
137
138
PyObject *
139
PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
140
29.7M
{
141
29.7M
    PyBytesObject *op;
142
29.7M
    if (size < 0) {
143
0
        PyErr_SetString(PyExc_SystemError,
144
0
            "Negative size passed to PyBytes_FromStringAndSize");
145
0
        return NULL;
146
0
    }
147
29.7M
    if (size == 1 && str != NULL) {
148
4.28M
        op = CHARACTER(*str & 255);
149
4.28M
        assert(_Py_IsImmortal(op));
150
4.28M
        return (PyObject *)op;
151
4.28M
    }
152
25.4M
    if (size == 0) {
153
6.25M
        return bytes_get_empty();
154
6.25M
    }
155
156
19.1M
    op = (PyBytesObject *)_PyBytes_FromSize(size, 0);
157
19.1M
    if (op == NULL)
158
0
        return NULL;
159
19.1M
    if (str == NULL)
160
4.66M
        return (PyObject *) op;
161
162
14.5M
    memcpy(op->ob_sval, str, size);
163
14.5M
    return (PyObject *) op;
164
19.1M
}
165
166
PyObject *
167
PyBytes_FromString(const char *str)
168
426
{
169
426
    size_t size;
170
426
    PyBytesObject *op;
171
172
426
    assert(str != NULL);
173
426
    size = strlen(str);
174
426
    if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
175
0
        PyErr_SetString(PyExc_OverflowError,
176
0
            "byte string is too long");
177
0
        return NULL;
178
0
    }
179
180
426
    if (size == 0) {
181
0
        return bytes_get_empty();
182
0
    }
183
426
    else if (size == 1) {
184
0
        op = CHARACTER(*str & 255);
185
0
        assert(_Py_IsImmortal(op));
186
0
        return (PyObject *)op;
187
0
    }
188
189
    /* Inline PyObject_NewVar */
190
426
    op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size);
191
426
    if (op == NULL) {
192
0
        return PyErr_NoMemory();
193
0
    }
194
426
    _PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
195
426
    set_ob_shash(op, -1);
196
426
    memcpy(op->ob_sval, str, size+1);
197
426
    return (PyObject *) op;
198
426
}
199
200
201
static char*
202
bytes_fromformat(PyBytesWriter *writer, Py_ssize_t writer_pos,
203
                 const char *format, va_list vargs)
204
0
{
205
0
    const char *f;
206
0
    const char *p;
207
0
    Py_ssize_t prec;
208
0
    int longflag;
209
0
    int size_tflag;
210
    /* Longest 64-bit formatted numbers:
211
       - "18446744073709551615\0" (21 bytes)
212
       - "-9223372036854775808\0" (21 bytes)
213
       Decimal takes the most space (it isn't enough for octal.)
214
215
       Longest 64-bit pointer representation:
216
       "0xffffffffffffffff\0" (19 bytes). */
217
0
    char buffer[21];
218
219
0
    char *s = (char*)PyBytesWriter_GetData(writer) + writer_pos;
220
221
0
#define WRITE_BYTES_LEN(str, len_expr) \
222
0
    do { \
223
0
        size_t len = (len_expr); \
224
0
        s = PyBytesWriter_GrowAndUpdatePointer(writer, len, s); \
225
0
        if (s == NULL) { \
226
0
            goto error; \
227
0
        } \
228
0
        memcpy(s, (str), len); \
229
0
        s += len; \
230
0
    } while (0)
231
0
#define WRITE_BYTES(str) WRITE_BYTES_LEN(str, strlen(str))
232
233
0
    for (f = format; *f; f++) {
234
0
        if (*f != '%') {
235
0
            *s++ = *f;
236
0
            continue;
237
0
        }
238
239
0
        p = f++;
240
241
        /* ignore the width (ex: 10 in "%10s") */
242
0
        while (Py_ISDIGIT(*f))
243
0
            f++;
244
245
        /* parse the precision (ex: 10 in "%.10s") */
246
0
        prec = 0;
247
0
        if (*f == '.') {
248
0
            f++;
249
0
            for (; Py_ISDIGIT(*f); f++) {
250
0
                prec = (prec * 10) + (*f - '0');
251
0
            }
252
0
        }
253
254
0
        while (*f && *f != '%' && !Py_ISALPHA(*f))
255
0
            f++;
256
257
        /* handle the long flag ('l'), but only for %ld and %lu.
258
           others can be added when necessary. */
259
0
        longflag = 0;
260
0
        if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) {
261
0
            longflag = 1;
262
0
            ++f;
263
0
        }
264
265
        /* handle the size_t flag ('z'). */
266
0
        size_tflag = 0;
267
0
        if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
268
0
            size_tflag = 1;
269
0
            ++f;
270
0
        }
271
272
0
        switch (*f) {
273
0
        case 'c':
274
0
        {
275
0
            int c = va_arg(vargs, int);
276
0
            if (c < 0 || c > 255) {
277
0
                PyErr_SetString(PyExc_OverflowError,
278
0
                                "PyBytes_FromFormatV(): %c format "
279
0
                                "expects an integer in range [0; 255]");
280
0
                goto error;
281
0
            }
282
0
            *s++ = (unsigned char)c;
283
0
            break;
284
0
        }
285
286
0
        case 'd':
287
0
            if (longflag) {
288
0
                sprintf(buffer, "%ld", va_arg(vargs, long));
289
0
            }
290
0
            else if (size_tflag) {
291
0
                sprintf(buffer, "%zd", va_arg(vargs, Py_ssize_t));
292
0
            }
293
0
            else {
294
0
                sprintf(buffer, "%d", va_arg(vargs, int));
295
0
            }
296
0
            assert(strlen(buffer) < sizeof(buffer));
297
0
            WRITE_BYTES(buffer);
298
0
            break;
299
300
0
        case 'u':
301
0
            if (longflag) {
302
0
                sprintf(buffer, "%lu", va_arg(vargs, unsigned long));
303
0
            }
304
0
            else if (size_tflag) {
305
0
                sprintf(buffer, "%zu", va_arg(vargs, size_t));
306
0
            }
307
0
            else {
308
0
                sprintf(buffer, "%u", va_arg(vargs, unsigned int));
309
0
            }
310
0
            assert(strlen(buffer) < sizeof(buffer));
311
0
            WRITE_BYTES(buffer);
312
0
            break;
313
314
0
        case 'i':
315
0
            sprintf(buffer, "%i", va_arg(vargs, int));
316
0
            assert(strlen(buffer) < sizeof(buffer));
317
0
            WRITE_BYTES(buffer);
318
0
            break;
319
320
0
        case 'x':
321
0
            sprintf(buffer, "%x", va_arg(vargs, int));
322
0
            assert(strlen(buffer) < sizeof(buffer));
323
0
            WRITE_BYTES(buffer);
324
0
            break;
325
326
0
        case 's':
327
0
        {
328
0
            Py_ssize_t i;
329
330
0
            p = va_arg(vargs, const char*);
331
0
            if (prec <= 0) {
332
0
                i = strlen(p);
333
0
            }
334
0
            else {
335
0
                i = 0;
336
0
                while (i < prec && p[i]) {
337
0
                    i++;
338
0
                }
339
0
            }
340
0
            WRITE_BYTES_LEN(p, i);
341
0
            break;
342
0
        }
343
344
0
        case 'p':
345
0
            sprintf(buffer, "%p", va_arg(vargs, void*));
346
0
            assert(strlen(buffer) < sizeof(buffer));
347
            /* %p is ill-defined:  ensure leading 0x. */
348
0
            if (buffer[1] == 'X')
349
0
                buffer[1] = 'x';
350
0
            else if (buffer[1] != 'x') {
351
0
                memmove(buffer+2, buffer, strlen(buffer)+1);
352
0
                buffer[0] = '0';
353
0
                buffer[1] = 'x';
354
0
            }
355
0
            WRITE_BYTES(buffer);
356
0
            break;
357
358
0
        case '%':
359
0
            *s++ = '%';
360
0
            break;
361
362
0
        default:
363
            /* invalid format string: copy unformatted string and exit */
364
0
            WRITE_BYTES(p);
365
0
            return s;
366
0
        }
367
0
    }
368
369
0
#undef WRITE_BYTES
370
0
#undef WRITE_BYTES_LEN
371
372
0
    return s;
373
374
0
 error:
375
0
    return NULL;
376
0
}
377
378
379
PyObject *
380
PyBytes_FromFormatV(const char *format, va_list vargs)
381
0
{
382
0
    Py_ssize_t alloc = strlen(format);
383
0
    PyBytesWriter *writer = PyBytesWriter_Create(alloc);
384
0
    if (writer == NULL) {
385
0
        return NULL;
386
0
    }
387
388
0
    char *s = bytes_fromformat(writer, 0, format, vargs);
389
0
    if (s == NULL) {
390
0
        PyBytesWriter_Discard(writer);
391
0
        return NULL;
392
0
    }
393
394
0
    return PyBytesWriter_FinishWithPointer(writer, s);
395
0
}
396
397
398
PyObject *
399
PyBytes_FromFormat(const char *format, ...)
400
0
{
401
0
    PyObject* ret;
402
0
    va_list vargs;
403
404
0
    va_start(vargs, format);
405
0
    ret = PyBytes_FromFormatV(format, vargs);
406
0
    va_end(vargs);
407
0
    return ret;
408
0
}
409
410
411
/* Helpers for formatstring */
412
413
Py_LOCAL_INLINE(PyObject *)
414
getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx)
415
0
{
416
0
    Py_ssize_t argidx = *p_argidx;
417
0
    if (argidx < arglen) {
418
0
        (*p_argidx)++;
419
0
        if (arglen < 0)
420
0
            return args;
421
0
        else
422
0
            return PyTuple_GetItem(args, argidx);
423
0
    }
424
0
    PyErr_SetString(PyExc_TypeError,
425
0
                    "not enough arguments for format string");
426
0
    return NULL;
427
0
}
428
429
/* Returns a new reference to a PyBytes object, or NULL on failure. */
430
431
static char*
432
formatfloat(PyObject *v, int flags, int prec, int type,
433
            PyObject **p_result, PyBytesWriter *writer, char *str)
434
0
{
435
0
    char *p;
436
0
    PyObject *result;
437
0
    double x;
438
0
    size_t len;
439
0
    int dtoa_flags = 0;
440
441
0
    x = PyFloat_AsDouble(v);
442
0
    if (x == -1.0 && PyErr_Occurred()) {
443
0
        PyErr_Format(PyExc_TypeError, "float argument required, "
444
0
                     "not %.200s", Py_TYPE(v)->tp_name);
445
0
        return NULL;
446
0
    }
447
448
0
    if (prec < 0)
449
0
        prec = 6;
450
451
0
    if (flags & F_ALT) {
452
0
        dtoa_flags |= Py_DTSF_ALT;
453
0
    }
454
0
    p = PyOS_double_to_string(x, type, prec, dtoa_flags, NULL);
455
456
0
    if (p == NULL)
457
0
        return NULL;
458
459
0
    len = strlen(p);
460
0
    if (writer != NULL) {
461
0
        str = PyBytesWriter_GrowAndUpdatePointer(writer, len, str);
462
0
        if (str == NULL) {
463
0
            PyMem_Free(p);
464
0
            return NULL;
465
0
        }
466
0
        memcpy(str, p, len);
467
0
        PyMem_Free(p);
468
0
        str += len;
469
0
        return str;
470
0
    }
471
472
0
    result = PyBytes_FromStringAndSize(p, len);
473
0
    PyMem_Free(p);
474
0
    *p_result = result;
475
0
    return result != NULL ? str : NULL;
476
0
}
477
478
static PyObject *
479
formatlong(PyObject *v, int flags, int prec, int type)
480
0
{
481
0
    PyObject *result, *iobj;
482
0
    if (PyLong_Check(v))
483
0
        return _PyUnicode_FormatLong(v, flags & F_ALT, prec, type);
484
0
    if (PyNumber_Check(v)) {
485
        /* make sure number is a type of integer for o, x, and X */
486
0
        if (type == 'o' || type == 'x' || type == 'X')
487
0
            iobj = _PyNumber_Index(v);
488
0
        else
489
0
            iobj = PyNumber_Long(v);
490
0
        if (iobj != NULL) {
491
0
            assert(PyLong_Check(iobj));
492
0
            result = _PyUnicode_FormatLong(iobj, flags & F_ALT, prec, type);
493
0
            Py_DECREF(iobj);
494
0
            return result;
495
0
        }
496
0
        if (!PyErr_ExceptionMatches(PyExc_TypeError))
497
0
            return NULL;
498
0
    }
499
0
    PyErr_Format(PyExc_TypeError,
500
0
        "%%%c format: %s is required, not %.200s", type,
501
0
        (type == 'o' || type == 'x' || type == 'X') ? "an integer"
502
0
                                                    : "a real number",
503
0
        Py_TYPE(v)->tp_name);
504
0
    return NULL;
505
0
}
506
507
static int
508
byte_converter(PyObject *arg, char *p)
509
0
{
510
0
    if (PyBytes_Check(arg)) {
511
0
        if (PyBytes_GET_SIZE(arg) != 1) {
512
0
            PyErr_Format(PyExc_TypeError,
513
0
                         "%%c requires an integer in range(256) or "
514
0
                         "a single byte, not a bytes object of length %zd",
515
0
                         PyBytes_GET_SIZE(arg));
516
0
            return 0;
517
0
        }
518
0
        *p = PyBytes_AS_STRING(arg)[0];
519
0
        return 1;
520
0
    }
521
0
    else if (PyByteArray_Check(arg)) {
522
0
        if (PyByteArray_GET_SIZE(arg) != 1) {
523
0
            PyErr_Format(PyExc_TypeError,
524
0
                         "%%c requires an integer in range(256) or "
525
0
                         "a single byte, not a bytearray object of length %zd",
526
0
                         PyByteArray_GET_SIZE(arg));
527
0
            return 0;
528
0
        }
529
0
        *p = PyByteArray_AS_STRING(arg)[0];
530
0
        return 1;
531
0
    }
532
0
    else if (PyIndex_Check(arg)) {
533
0
        int overflow;
534
0
        long ival = PyLong_AsLongAndOverflow(arg, &overflow);
535
0
        if (ival == -1 && PyErr_Occurred()) {
536
0
            return 0;
537
0
        }
538
0
        if (!(0 <= ival && ival <= 255)) {
539
            /* this includes an overflow in converting to C long */
540
0
            PyErr_SetString(PyExc_OverflowError,
541
0
                            "%c arg not in range(256)");
542
0
            return 0;
543
0
        }
544
0
        *p = (char)ival;
545
0
        return 1;
546
0
    }
547
0
    PyErr_Format(PyExc_TypeError,
548
0
        "%%c requires an integer in range(256) or a single byte, not %T",
549
0
        arg);
550
0
    return 0;
551
0
}
552
553
static PyObject *_PyBytes_FromBuffer(PyObject *x);
554
555
static PyObject *
556
format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
557
0
{
558
0
    PyObject *func, *result;
559
    /* is it a bytes object? */
560
0
    if (PyBytes_Check(v)) {
561
0
        *pbuf = PyBytes_AS_STRING(v);
562
0
        *plen = PyBytes_GET_SIZE(v);
563
0
        return Py_NewRef(v);
564
0
    }
565
0
    if (PyByteArray_Check(v)) {
566
0
        *pbuf = PyByteArray_AS_STRING(v);
567
0
        *plen = PyByteArray_GET_SIZE(v);
568
0
        return Py_NewRef(v);
569
0
    }
570
    /* does it support __bytes__? */
571
0
    func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__));
572
0
    if (func != NULL) {
573
0
        result = _PyObject_CallNoArgs(func);
574
0
        Py_DECREF(func);
575
0
        if (result == NULL)
576
0
            return NULL;
577
0
        if (!PyBytes_Check(result)) {
578
0
            PyErr_Format(PyExc_TypeError,
579
0
                         "%T.__bytes__() must return a bytes, not %T",
580
0
                         v, result);
581
0
            Py_DECREF(result);
582
0
            return NULL;
583
0
        }
584
0
        *pbuf = PyBytes_AS_STRING(result);
585
0
        *plen = PyBytes_GET_SIZE(result);
586
0
        return result;
587
0
    }
588
    /* does it support buffer protocol? */
589
0
    if (PyObject_CheckBuffer(v)) {
590
        /* maybe we can avoid making a copy of the buffer object here? */
591
0
        result = _PyBytes_FromBuffer(v);
592
0
        if (result == NULL)
593
0
            return NULL;
594
0
        *pbuf = PyBytes_AS_STRING(result);
595
0
        *plen = PyBytes_GET_SIZE(result);
596
0
        return result;
597
0
    }
598
0
    PyErr_Format(PyExc_TypeError,
599
0
                 "%%b requires a bytes-like object, "
600
0
                 "or an object that implements __bytes__, not '%.100s'",
601
0
                 Py_TYPE(v)->tp_name);
602
0
    return NULL;
603
0
}
604
605
/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */
606
607
PyObject *
608
_PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
609
                  PyObject *args, int use_bytearray)
610
0
{
611
0
    const char *fmt;
612
0
    Py_ssize_t arglen, argidx;
613
0
    Py_ssize_t fmtcnt;
614
0
    int args_owned = 0;
615
0
    PyObject *dict = NULL;
616
617
0
    if (args == NULL) {
618
0
        PyErr_BadInternalCall();
619
0
        return NULL;
620
0
    }
621
0
    fmt = format;
622
0
    fmtcnt = format_len;
623
624
0
    PyBytesWriter *writer;
625
0
    if (use_bytearray) {
626
0
        writer = _PyBytesWriter_CreateByteArray(fmtcnt);
627
0
    }
628
0
    else {
629
0
        writer = PyBytesWriter_Create(fmtcnt);
630
0
    }
631
0
    if (writer == NULL) {
632
0
        return NULL;
633
0
    }
634
0
    char *res = PyBytesWriter_GetData(writer);
635
636
0
    if (PyTuple_Check(args)) {
637
0
        arglen = PyTuple_GET_SIZE(args);
638
0
        argidx = 0;
639
0
    }
640
0
    else {
641
0
        arglen = -1;
642
0
        argidx = -2;
643
0
    }
644
0
    if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript &&
645
0
        !PyTuple_Check(args) && !PyBytes_Check(args) && !PyUnicode_Check(args) &&
646
0
        !PyByteArray_Check(args)) {
647
0
            dict = args;
648
0
    }
649
650
0
    while (--fmtcnt >= 0) {
651
0
        if (*fmt != '%') {
652
0
            Py_ssize_t len;
653
0
            char *pos;
654
655
0
            pos = (char *)memchr(fmt + 1, '%', fmtcnt);
656
0
            if (pos != NULL)
657
0
                len = pos - fmt;
658
0
            else
659
0
                len = fmtcnt + 1;
660
0
            assert(len != 0);
661
662
0
            memcpy(res, fmt, len);
663
0
            res += len;
664
0
            fmt += len;
665
0
            fmtcnt -= (len - 1);
666
0
        }
667
0
        else {
668
            /* Got a format specifier */
669
0
            int flags = 0;
670
0
            Py_ssize_t width = -1;
671
0
            int prec = -1;
672
0
            int c = '\0';
673
0
            int fill;
674
0
            PyObject *v = NULL;
675
0
            PyObject *temp = NULL;
676
0
            const char *pbuf = NULL;
677
0
            int sign;
678
0
            Py_ssize_t len = 0;
679
0
            char onechar; /* For byte_converter() */
680
0
            Py_ssize_t alloc;
681
682
0
            fmt++;
683
0
            if (*fmt == '%') {
684
0
                *res++ = '%';
685
0
                fmt++;
686
0
                fmtcnt--;
687
0
                continue;
688
0
            }
689
0
            if (*fmt == '(') {
690
0
                const char *keystart;
691
0
                Py_ssize_t keylen;
692
0
                PyObject *key;
693
0
                int pcount = 1;
694
695
0
                if (dict == NULL) {
696
0
                    PyErr_SetString(PyExc_TypeError,
697
0
                             "format requires a mapping");
698
0
                    goto error;
699
0
                }
700
0
                ++fmt;
701
0
                --fmtcnt;
702
0
                keystart = fmt;
703
                /* Skip over balanced parentheses */
704
0
                while (pcount > 0 && --fmtcnt >= 0) {
705
0
                    if (*fmt == ')')
706
0
                        --pcount;
707
0
                    else if (*fmt == '(')
708
0
                        ++pcount;
709
0
                    fmt++;
710
0
                }
711
0
                keylen = fmt - keystart - 1;
712
0
                if (fmtcnt < 0 || pcount > 0) {
713
0
                    PyErr_SetString(PyExc_ValueError,
714
0
                               "incomplete format key");
715
0
                    goto error;
716
0
                }
717
0
                key = PyBytes_FromStringAndSize(keystart,
718
0
                                                 keylen);
719
0
                if (key == NULL)
720
0
                    goto error;
721
0
                if (args_owned) {
722
0
                    Py_DECREF(args);
723
0
                    args_owned = 0;
724
0
                }
725
0
                args = PyObject_GetItem(dict, key);
726
0
                Py_DECREF(key);
727
0
                if (args == NULL) {
728
0
                    goto error;
729
0
                }
730
0
                args_owned = 1;
731
0
                arglen = -1;
732
0
                argidx = -2;
733
0
            }
734
735
            /* Parse flags. Example: "%+i" => flags=F_SIGN. */
736
0
            while (--fmtcnt >= 0) {
737
0
                switch (c = *fmt++) {
738
0
                case '-': flags |= F_LJUST; continue;
739
0
                case '+': flags |= F_SIGN; continue;
740
0
                case ' ': flags |= F_BLANK; continue;
741
0
                case '#': flags |= F_ALT; continue;
742
0
                case '0': flags |= F_ZERO; continue;
743
0
                }
744
0
                break;
745
0
            }
746
747
            /* Parse width. Example: "%10s" => width=10 */
748
0
            if (c == '*') {
749
0
                v = getnextarg(args, arglen, &argidx);
750
0
                if (v == NULL)
751
0
                    goto error;
752
0
                if (!PyLong_Check(v)) {
753
0
                    PyErr_SetString(PyExc_TypeError,
754
0
                                    "* wants int");
755
0
                    goto error;
756
0
                }
757
0
                width = PyLong_AsSsize_t(v);
758
0
                if (width == -1 && PyErr_Occurred())
759
0
                    goto error;
760
0
                if (width < 0) {
761
0
                    flags |= F_LJUST;
762
0
                    width = -width;
763
0
                }
764
0
                if (--fmtcnt >= 0)
765
0
                    c = *fmt++;
766
0
            }
767
0
            else if (c >= 0 && Py_ISDIGIT(c)) {
768
0
                width = c - '0';
769
0
                while (--fmtcnt >= 0) {
770
0
                    c = Py_CHARMASK(*fmt++);
771
0
                    if (!Py_ISDIGIT(c))
772
0
                        break;
773
0
                    if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) {
774
0
                        PyErr_SetString(
775
0
                            PyExc_ValueError,
776
0
                            "width too big");
777
0
                        goto error;
778
0
                    }
779
0
                    width = width*10 + (c - '0');
780
0
                }
781
0
            }
782
783
            /* Parse precision. Example: "%.3f" => prec=3 */
784
0
            if (c == '.') {
785
0
                prec = 0;
786
0
                if (--fmtcnt >= 0)
787
0
                    c = *fmt++;
788
0
                if (c == '*') {
789
0
                    v = getnextarg(args, arglen, &argidx);
790
0
                    if (v == NULL)
791
0
                        goto error;
792
0
                    if (!PyLong_Check(v)) {
793
0
                        PyErr_SetString(
794
0
                            PyExc_TypeError,
795
0
                            "* wants int");
796
0
                        goto error;
797
0
                    }
798
0
                    prec = PyLong_AsInt(v);
799
0
                    if (prec == -1 && PyErr_Occurred())
800
0
                        goto error;
801
0
                    if (prec < 0)
802
0
                        prec = 0;
803
0
                    if (--fmtcnt >= 0)
804
0
                        c = *fmt++;
805
0
                }
806
0
                else if (c >= 0 && Py_ISDIGIT(c)) {
807
0
                    prec = c - '0';
808
0
                    while (--fmtcnt >= 0) {
809
0
                        c = Py_CHARMASK(*fmt++);
810
0
                        if (!Py_ISDIGIT(c))
811
0
                            break;
812
0
                        if (prec > (INT_MAX - ((int)c - '0')) / 10) {
813
0
                            PyErr_SetString(
814
0
                                PyExc_ValueError,
815
0
                                "prec too big");
816
0
                            goto error;
817
0
                        }
818
0
                        prec = prec*10 + (c - '0');
819
0
                    }
820
0
                }
821
0
            } /* prec */
822
0
            if (fmtcnt >= 0) {
823
0
                if (c == 'h' || c == 'l' || c == 'L') {
824
0
                    if (--fmtcnt >= 0)
825
0
                        c = *fmt++;
826
0
                }
827
0
            }
828
0
            if (fmtcnt < 0) {
829
0
                PyErr_SetString(PyExc_ValueError,
830
0
                                "incomplete format");
831
0
                goto error;
832
0
            }
833
0
            v = getnextarg(args, arglen, &argidx);
834
0
            if (v == NULL)
835
0
                goto error;
836
837
0
            if (fmtcnt == 0) {
838
                /* last write: disable writer overallocation */
839
0
                writer->overallocate = 0;
840
0
            }
841
842
0
            sign = 0;
843
0
            fill = ' ';
844
0
            switch (c) {
845
0
            case 'r':
846
                // %r is only for 2/3 code; 3 only code should use %a
847
0
            case 'a':
848
0
                temp = PyObject_ASCII(v);
849
0
                if (temp == NULL)
850
0
                    goto error;
851
0
                assert(PyUnicode_IS_ASCII(temp));
852
0
                pbuf = (const char *)PyUnicode_1BYTE_DATA(temp);
853
0
                len = PyUnicode_GET_LENGTH(temp);
854
0
                if (prec >= 0 && len > prec)
855
0
                    len = prec;
856
0
                break;
857
858
0
            case 's':
859
                // %s is only for 2/3 code; 3 only code should use %b
860
0
            case 'b':
861
0
                temp = format_obj(v, &pbuf, &len);
862
0
                if (temp == NULL)
863
0
                    goto error;
864
0
                if (prec >= 0 && len > prec)
865
0
                    len = prec;
866
0
                break;
867
868
0
            case 'i':
869
0
            case 'd':
870
0
            case 'u':
871
0
            case 'o':
872
0
            case 'x':
873
0
            case 'X':
874
0
                if (PyLong_CheckExact(v)
875
0
                    && width == -1 && prec == -1
876
0
                    && !(flags & (F_SIGN | F_BLANK))
877
0
                    && c != 'X')
878
0
                {
879
                    /* Fast path */
880
0
                    int alternate = flags & F_ALT;
881
0
                    int base;
882
883
0
                    switch(c)
884
0
                    {
885
0
                        default:
886
0
                            Py_UNREACHABLE();
887
0
                        case 'd':
888
0
                        case 'i':
889
0
                        case 'u':
890
0
                            base = 10;
891
0
                            break;
892
0
                        case 'o':
893
0
                            base = 8;
894
0
                            break;
895
0
                        case 'x':
896
0
                        case 'X':
897
0
                            base = 16;
898
0
                            break;
899
0
                    }
900
901
                    /* Fast path */
902
0
                    res = _PyLong_FormatBytesWriter(writer, res,
903
0
                                                    v, base, alternate);
904
0
                    if (res == NULL)
905
0
                        goto error;
906
0
                    continue;
907
0
                }
908
909
0
                temp = formatlong(v, flags, prec, c);
910
0
                if (!temp)
911
0
                    goto error;
912
0
                assert(PyUnicode_IS_ASCII(temp));
913
0
                pbuf = (const char *)PyUnicode_1BYTE_DATA(temp);
914
0
                len = PyUnicode_GET_LENGTH(temp);
915
0
                sign = 1;
916
0
                if (flags & F_ZERO)
917
0
                    fill = '0';
918
0
                break;
919
920
0
            case 'e':
921
0
            case 'E':
922
0
            case 'f':
923
0
            case 'F':
924
0
            case 'g':
925
0
            case 'G':
926
0
                if (width == -1 && prec == -1
927
0
                    && !(flags & (F_SIGN | F_BLANK)))
928
0
                {
929
                    /* Fast path */
930
0
                    res = formatfloat(v, flags, prec, c, NULL, writer, res);
931
0
                    if (res == NULL)
932
0
                        goto error;
933
0
                    continue;
934
0
                }
935
936
0
                if (!formatfloat(v, flags, prec, c, &temp, NULL, res))
937
0
                    goto error;
938
0
                pbuf = PyBytes_AS_STRING(temp);
939
0
                len = PyBytes_GET_SIZE(temp);
940
0
                sign = 1;
941
0
                if (flags & F_ZERO)
942
0
                    fill = '0';
943
0
                break;
944
945
0
            case 'c':
946
0
                pbuf = &onechar;
947
0
                len = byte_converter(v, &onechar);
948
0
                if (!len)
949
0
                    goto error;
950
0
                if (width == -1) {
951
                    /* Fast path */
952
0
                    *res++ = onechar;
953
0
                    continue;
954
0
                }
955
0
                break;
956
957
0
            default:
958
0
                PyErr_Format(PyExc_ValueError,
959
0
                  "unsupported format character '%c' (0x%x) "
960
0
                  "at index %zd",
961
0
                  c, c,
962
0
                  (Py_ssize_t)(fmt - 1 - format));
963
0
                goto error;
964
0
            }
965
966
0
            if (sign) {
967
0
                if (*pbuf == '-' || *pbuf == '+') {
968
0
                    sign = *pbuf++;
969
0
                    len--;
970
0
                }
971
0
                else if (flags & F_SIGN)
972
0
                    sign = '+';
973
0
                else if (flags & F_BLANK)
974
0
                    sign = ' ';
975
0
                else
976
0
                    sign = 0;
977
0
            }
978
0
            if (width < len)
979
0
                width = len;
980
981
0
            alloc = width;
982
0
            if (sign != 0 && len == width)
983
0
                alloc++;
984
            /* 2: size preallocated for %s */
985
0
            if (alloc > 2) {
986
0
                res = PyBytesWriter_GrowAndUpdatePointer(writer, alloc - 2, res);
987
0
                if (res == NULL) {
988
0
                    goto error;
989
0
                }
990
0
            }
991
#ifndef NDEBUG
992
            char *before = res;
993
#endif
994
995
            /* Write the sign if needed */
996
0
            if (sign) {
997
0
                if (fill != ' ')
998
0
                    *res++ = sign;
999
0
                if (width > len)
1000
0
                    width--;
1001
0
            }
1002
1003
            /* Write the numeric prefix for "x", "X" and "o" formats
1004
               if the alternate form is used.
1005
               For example, write "0x" for the "%#x" format. */
1006
0
            if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) {
1007
0
                assert(pbuf[0] == '0');
1008
0
                assert(pbuf[1] == c);
1009
0
                if (fill != ' ') {
1010
0
                    *res++ = *pbuf++;
1011
0
                    *res++ = *pbuf++;
1012
0
                }
1013
0
                width -= 2;
1014
0
                if (width < 0)
1015
0
                    width = 0;
1016
0
                len -= 2;
1017
0
            }
1018
1019
            /* Pad left with the fill character if needed */
1020
0
            if (width > len && !(flags & F_LJUST)) {
1021
0
                memset(res, fill, width - len);
1022
0
                res += (width - len);
1023
0
                width = len;
1024
0
            }
1025
1026
            /* If padding with spaces: write sign if needed and/or numeric
1027
               prefix if the alternate form is used */
1028
0
            if (fill == ' ') {
1029
0
                if (sign)
1030
0
                    *res++ = sign;
1031
0
                if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) {
1032
0
                    assert(pbuf[0] == '0');
1033
0
                    assert(pbuf[1] == c);
1034
0
                    *res++ = *pbuf++;
1035
0
                    *res++ = *pbuf++;
1036
0
                }
1037
0
            }
1038
1039
            /* Copy bytes */
1040
0
            memcpy(res, pbuf, len);
1041
0
            res += len;
1042
1043
            /* Pad right with the fill character if needed */
1044
0
            if (width > len) {
1045
0
                memset(res, ' ', width - len);
1046
0
                res += (width - len);
1047
0
            }
1048
1049
0
            if (dict && (argidx < arglen)) {
1050
0
                PyErr_SetString(PyExc_TypeError,
1051
0
                           "not all arguments converted during bytes formatting");
1052
0
                Py_XDECREF(temp);
1053
0
                goto error;
1054
0
            }
1055
0
            Py_XDECREF(temp);
1056
1057
#ifndef NDEBUG
1058
            /* check that we computed the exact size for this write */
1059
            assert((res - before) == alloc);
1060
#endif
1061
0
        } /* '%' */
1062
1063
        /* If overallocation was disabled, ensure that it was the last
1064
           write. Otherwise, we missed an optimization */
1065
0
        assert(writer->overallocate || fmtcnt == 0 || use_bytearray);
1066
0
    } /* until end */
1067
1068
0
    if (argidx < arglen && !dict) {
1069
0
        PyErr_SetString(PyExc_TypeError,
1070
0
                        "not all arguments converted during bytes formatting");
1071
0
        goto error;
1072
0
    }
1073
1074
0
    if (args_owned) {
1075
0
        Py_DECREF(args);
1076
0
    }
1077
0
    return PyBytesWriter_FinishWithPointer(writer, res);
1078
1079
0
 error:
1080
0
    PyBytesWriter_Discard(writer);
1081
0
    if (args_owned) {
1082
0
        Py_DECREF(args);
1083
0
    }
1084
0
    return NULL;
1085
0
}
1086
1087
/* Unescape a backslash-escaped string. */
1088
PyObject *_PyBytes_DecodeEscape2(const char *s,
1089
                                Py_ssize_t len,
1090
                                const char *errors,
1091
                                int *first_invalid_escape_char,
1092
                                const char **first_invalid_escape_ptr)
1093
2.93k
{
1094
2.93k
    PyBytesWriter *writer = PyBytesWriter_Create(len);
1095
2.93k
    if (writer == NULL) {
1096
0
        return NULL;
1097
0
    }
1098
2.93k
    char *p = PyBytesWriter_GetData(writer);
1099
1100
2.93k
    *first_invalid_escape_char = -1;
1101
2.93k
    *first_invalid_escape_ptr = NULL;
1102
1103
2.93k
    const char *end = s + len;
1104
55.4k
    while (s < end) {
1105
52.5k
        if (*s != '\\') {
1106
41.0k
            *p++ = *s++;
1107
41.0k
            continue;
1108
41.0k
        }
1109
1110
11.4k
        s++;
1111
11.4k
        if (s == end) {
1112
0
            PyErr_SetString(PyExc_ValueError,
1113
0
                            "Trailing \\ in string");
1114
0
            goto failed;
1115
0
        }
1116
1117
11.4k
        switch (*s++) {
1118
        /* XXX This assumes ASCII! */
1119
1.36k
        case '\n': break;
1120
652
        case '\\': *p++ = '\\'; break;
1121
219
        case '\'': *p++ = '\''; break;
1122
706
        case '\"': *p++ = '\"'; break;
1123
219
        case 'b': *p++ = '\b'; break;
1124
303
        case 'f': *p++ = '\014'; break; /* FF */
1125
225
        case 't': *p++ = '\t'; break;
1126
317
        case 'n': *p++ = '\n'; break;
1127
468
        case 'r': *p++ = '\r'; break;
1128
231
        case 'v': *p++ = '\013'; break; /* VT */
1129
201
        case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1130
2.20k
        case '0': case '1': case '2': case '3':
1131
4.97k
        case '4': case '5': case '6': case '7':
1132
4.97k
        {
1133
4.97k
            int c = s[-1] - '0';
1134
4.97k
            if (s < end && '0' <= *s && *s <= '7') {
1135
2.41k
                c = (c<<3) + *s++ - '0';
1136
2.41k
                if (s < end && '0' <= *s && *s <= '7')
1137
1.26k
                    c = (c<<3) + *s++ - '0';
1138
2.41k
            }
1139
4.97k
            if (c > 0377) {
1140
947
                if (*first_invalid_escape_char == -1) {
1141
149
                    *first_invalid_escape_char = c;
1142
                    /* Back up 3 chars, since we've already incremented s. */
1143
149
                    *first_invalid_escape_ptr = s - 3;
1144
149
                }
1145
947
            }
1146
4.97k
            *p++ = c;
1147
4.97k
            break;
1148
4.58k
        }
1149
304
        case 'x':
1150
304
            if (s+1 < end) {
1151
303
                int digit1, digit2;
1152
303
                digit1 = _PyLong_DigitValue[Py_CHARMASK(s[0])];
1153
303
                digit2 = _PyLong_DigitValue[Py_CHARMASK(s[1])];
1154
303
                if (digit1 < 16 && digit2 < 16) {
1155
300
                    *p++ = (unsigned char)((digit1 << 4) + digit2);
1156
300
                    s += 2;
1157
300
                    break;
1158
300
                }
1159
303
            }
1160
            /* invalid hexadecimal digits */
1161
1162
4
            if (!errors || strcmp(errors, "strict") == 0) {
1163
4
                PyErr_Format(PyExc_ValueError,
1164
4
                             "invalid \\x escape at position %zd",
1165
4
                             s - 2 - (end - len));
1166
4
                goto failed;
1167
4
            }
1168
0
            if (strcmp(errors, "replace") == 0) {
1169
0
                *p++ = '?';
1170
0
            } else if (strcmp(errors, "ignore") == 0)
1171
0
                /* do nothing */;
1172
0
            else {
1173
0
                PyErr_Format(PyExc_ValueError,
1174
0
                             "decoding error; unknown "
1175
0
                             "error handling code: %.400s",
1176
0
                             errors);
1177
0
                goto failed;
1178
0
            }
1179
            /* skip \x */
1180
0
            if (s < end && Py_ISXDIGIT(s[0]))
1181
0
                s++; /* and a hexdigit */
1182
0
            break;
1183
1184
1.30k
        default:
1185
1.30k
            if (*first_invalid_escape_char == -1) {
1186
720
                *first_invalid_escape_char = (unsigned char)s[-1];
1187
                /* Back up one char, since we've already incremented s. */
1188
720
                *first_invalid_escape_ptr = s - 1;
1189
720
            }
1190
1.30k
            *p++ = '\\';
1191
1.30k
            s--;
1192
11.4k
        }
1193
11.4k
    }
1194
1195
2.92k
    return PyBytesWriter_FinishWithPointer(writer, p);
1196
1197
4
  failed:
1198
4
    PyBytesWriter_Discard(writer);
1199
4
    return NULL;
1200
2.93k
}
1201
1202
PyObject *PyBytes_DecodeEscape(const char *s,
1203
                                Py_ssize_t len,
1204
                                const char *errors,
1205
                                Py_ssize_t Py_UNUSED(unicode),
1206
                                const char *Py_UNUSED(recode_encoding))
1207
0
{
1208
0
    int first_invalid_escape_char;
1209
0
    const char *first_invalid_escape_ptr;
1210
0
    PyObject *result = _PyBytes_DecodeEscape2(s, len, errors,
1211
0
                                             &first_invalid_escape_char,
1212
0
                                             &first_invalid_escape_ptr);
1213
0
    if (result == NULL)
1214
0
        return NULL;
1215
0
    if (first_invalid_escape_char != -1) {
1216
0
        if (first_invalid_escape_char > 0xff) {
1217
0
            if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1218
0
                                 "b\"\\%o\" is an invalid octal escape sequence. "
1219
0
                                 "Such sequences will not work in the future. ",
1220
0
                                 first_invalid_escape_char) < 0)
1221
0
            {
1222
0
                Py_DECREF(result);
1223
0
                return NULL;
1224
0
            }
1225
0
        }
1226
0
        else {
1227
0
            if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1228
0
                                 "b\"\\%c\" is an invalid escape sequence. "
1229
0
                                 "Such sequences will not work in the future. ",
1230
0
                                 first_invalid_escape_char) < 0)
1231
0
            {
1232
0
                Py_DECREF(result);
1233
0
                return NULL;
1234
0
            }
1235
0
        }
1236
0
    }
1237
0
    return result;
1238
0
}
1239
/* -------------------------------------------------------------------- */
1240
/* object api */
1241
1242
Py_ssize_t
1243
PyBytes_Size(PyObject *op)
1244
5.12k
{
1245
5.12k
    if (!PyBytes_Check(op)) {
1246
0
        PyErr_Format(PyExc_TypeError,
1247
0
             "expected bytes, %.200s found", Py_TYPE(op)->tp_name);
1248
0
        return -1;
1249
0
    }
1250
5.12k
    return Py_SIZE(op);
1251
5.12k
}
1252
1253
char *
1254
PyBytes_AsString(PyObject *op)
1255
3.14M
{
1256
3.14M
    if (!PyBytes_Check(op)) {
1257
0
        PyErr_Format(PyExc_TypeError,
1258
0
             "expected bytes, %.200s found", Py_TYPE(op)->tp_name);
1259
0
        return NULL;
1260
0
    }
1261
3.14M
    return ((PyBytesObject *)op)->ob_sval;
1262
3.14M
}
1263
1264
int
1265
PyBytes_AsStringAndSize(PyObject *obj,
1266
                         char **s,
1267
                         Py_ssize_t *len)
1268
77.2k
{
1269
77.2k
    if (s == NULL) {
1270
0
        PyErr_BadInternalCall();
1271
0
        return -1;
1272
0
    }
1273
1274
77.2k
    if (!PyBytes_Check(obj)) {
1275
0
        PyErr_Format(PyExc_TypeError,
1276
0
             "expected bytes, %.200s found", Py_TYPE(obj)->tp_name);
1277
0
        return -1;
1278
0
    }
1279
1280
77.2k
    *s = PyBytes_AS_STRING(obj);
1281
77.2k
    if (len != NULL)
1282
77.2k
        *len = PyBytes_GET_SIZE(obj);
1283
0
    else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) {
1284
0
        PyErr_SetString(PyExc_ValueError,
1285
0
                        "embedded null byte");
1286
0
        return -1;
1287
0
    }
1288
77.2k
    return 0;
1289
77.2k
}
1290
1291
/* -------------------------------------------------------------------- */
1292
/* Methods */
1293
1294
0
#define STRINGLIB_GET_EMPTY() bytes_get_empty()
1295
1296
#include "stringlib/stringdefs.h"
1297
#define STRINGLIB_MUTABLE 0
1298
1299
#include "stringlib/fastsearch.h"
1300
#include "stringlib/count.h"
1301
#include "stringlib/find.h"
1302
#include "stringlib/join.h"
1303
#include "stringlib/partition.h"
1304
#include "stringlib/split.h"
1305
#include "stringlib/ctype.h"
1306
1307
#include "stringlib/transmogrify.h"
1308
1309
#undef STRINGLIB_GET_EMPTY
1310
1311
Py_ssize_t
1312
_PyBytes_Find(const char *haystack, Py_ssize_t len_haystack,
1313
              const char *needle, Py_ssize_t len_needle,
1314
              Py_ssize_t offset)
1315
0
{
1316
0
    assert(len_haystack >= 0);
1317
0
    assert(len_needle >= 0);
1318
    // Extra checks because stringlib_find accesses haystack[len_haystack].
1319
0
    if (len_needle == 0) {
1320
0
        return offset;
1321
0
    }
1322
0
    if (len_needle > len_haystack) {
1323
0
        return -1;
1324
0
    }
1325
0
    assert(len_haystack >= 1);
1326
0
    Py_ssize_t res = stringlib_find(haystack, len_haystack - 1,
1327
0
                                    needle, len_needle, offset);
1328
0
    if (res == -1) {
1329
0
        Py_ssize_t last_align = len_haystack - len_needle;
1330
0
        if (memcmp(haystack + last_align, needle, len_needle) == 0) {
1331
0
            return offset + last_align;
1332
0
        }
1333
0
    }
1334
0
    return res;
1335
0
}
1336
1337
Py_ssize_t
1338
_PyBytes_ReverseFind(const char *haystack, Py_ssize_t len_haystack,
1339
                     const char *needle, Py_ssize_t len_needle,
1340
                     Py_ssize_t offset)
1341
0
{
1342
0
    return stringlib_rfind(haystack, len_haystack,
1343
0
                           needle, len_needle, offset);
1344
0
}
1345
1346
PyObject *
1347
PyBytes_Repr(PyObject *obj, int smartquotes)
1348
2.29k
{
1349
2.29k
    return _Py_bytes_repr(PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj),
1350
2.29k
                          smartquotes, "bytes");
1351
2.29k
}
1352
1353
PyObject *
1354
_Py_bytes_repr(const char *data, Py_ssize_t length, int smartquotes,
1355
               const char *classname)
1356
2.29k
{
1357
2.29k
    Py_ssize_t i;
1358
2.29k
    Py_ssize_t newsize, squotes, dquotes;
1359
2.29k
    PyObject *v;
1360
2.29k
    unsigned char quote;
1361
2.29k
    Py_UCS1 *p;
1362
1363
    /* Compute size of output string */
1364
2.29k
    squotes = dquotes = 0;
1365
2.29k
    newsize = 3; /* b'' */
1366
35.0k
    for (i = 0; i < length; i++) {
1367
32.8k
        unsigned char c = data[i];
1368
32.8k
        Py_ssize_t incr = 1;
1369
32.8k
        switch(c) {
1370
852
        case '\'': squotes++; break;
1371
583
        case '"':  dquotes++; break;
1372
1.18k
        case '\\': case '\t': case '\n': case '\r':
1373
1.18k
            incr = 2; break; /* \C */
1374
30.1k
        default:
1375
30.1k
            if (c < ' ' || c >= 0x7f)
1376
855
                incr = 4; /* \xHH */
1377
32.8k
        }
1378
32.8k
        if (newsize > PY_SSIZE_T_MAX - incr)
1379
0
            goto overflow;
1380
32.8k
        newsize += incr;
1381
32.8k
    }
1382
2.29k
    quote = '\'';
1383
2.29k
    if (smartquotes && squotes && !dquotes)
1384
94
        quote = '"';
1385
2.29k
    if (squotes && quote == '\'') {
1386
279
        if (newsize > PY_SSIZE_T_MAX - squotes)
1387
0
            goto overflow;
1388
279
        newsize += squotes;
1389
279
    }
1390
1391
2.29k
    v = PyUnicode_New(newsize, 127);
1392
2.29k
    if (v == NULL) {
1393
0
        return NULL;
1394
0
    }
1395
2.29k
    p = PyUnicode_1BYTE_DATA(v);
1396
1397
2.29k
    *p++ = 'b', *p++ = quote;
1398
35.0k
    for (i = 0; i < length; i++) {
1399
32.8k
        unsigned char c = data[i];
1400
32.8k
        if (c == quote || c == '\\')
1401
895
            *p++ = '\\', *p++ = c;
1402
31.9k
        else if (c == '\t')
1403
261
            *p++ = '\\', *p++ = 't';
1404
31.6k
        else if (c == '\n')
1405
273
            *p++ = '\\', *p++ = 'n';
1406
31.3k
        else if (c == '\r')
1407
275
            *p++ = '\\', *p++ = 'r';
1408
31.1k
        else if (c < ' ' || c >= 0x7f) {
1409
855
            *p++ = '\\';
1410
855
            *p++ = 'x';
1411
855
            *p++ = Py_hexdigits[(c & 0xf0) >> 4];
1412
855
            *p++ = Py_hexdigits[c & 0xf];
1413
855
        }
1414
30.2k
        else
1415
30.2k
            *p++ = c;
1416
32.8k
    }
1417
2.29k
    *p++ = quote;
1418
2.29k
    assert(_PyUnicode_CheckConsistency(v, 1));
1419
2.29k
    return v;
1420
1421
0
  overflow:
1422
0
    PyErr_Format(PyExc_OverflowError,
1423
0
                 "%s object is too large to make repr", classname);
1424
0
    return NULL;
1425
2.29k
}
1426
1427
static PyObject *
1428
bytes_repr(PyObject *op)
1429
2.29k
{
1430
2.29k
    return PyBytes_Repr(op, 1);
1431
2.29k
}
1432
1433
static PyObject *
1434
bytes_str(PyObject *op)
1435
0
{
1436
0
    if (_Py_GetConfig()->bytes_warning) {
1437
0
        if (PyErr_WarnEx(PyExc_BytesWarning,
1438
0
                         "str() on a bytes instance", 1)) {
1439
0
            return NULL;
1440
0
        }
1441
0
    }
1442
0
    return bytes_repr(op);
1443
0
}
1444
1445
static Py_ssize_t
1446
bytes_length(PyObject *self)
1447
1.71M
{
1448
1.71M
    PyBytesObject *a = _PyBytes_CAST(self);
1449
1.71M
    return Py_SIZE(a);
1450
1.71M
}
1451
1452
/* This is also used by PyBytes_Concat() */
1453
static PyObject *
1454
bytes_concat(PyObject *a, PyObject *b)
1455
61.4k
{
1456
61.4k
    Py_buffer va, vb;
1457
61.4k
    PyObject *result = NULL;
1458
1459
61.4k
    va.len = -1;
1460
61.4k
    vb.len = -1;
1461
61.4k
    if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
1462
61.4k
        PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
1463
0
        PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
1464
0
                     Py_TYPE(b)->tp_name, Py_TYPE(a)->tp_name);
1465
0
        goto done;
1466
0
    }
1467
1468
    /* Optimize end cases */
1469
61.4k
    if (va.len == 0 && PyBytes_CheckExact(b)) {
1470
1.92k
        result = Py_NewRef(b);
1471
1.92k
        goto done;
1472
1.92k
    }
1473
59.5k
    if (vb.len == 0 && PyBytes_CheckExact(a)) {
1474
9.75k
        result = Py_NewRef(a);
1475
9.75k
        goto done;
1476
9.75k
    }
1477
1478
49.8k
    if (va.len > PY_SSIZE_T_MAX - vb.len) {
1479
0
        PyErr_NoMemory();
1480
0
        goto done;
1481
0
    }
1482
1483
49.8k
    result = PyBytes_FromStringAndSize(NULL, va.len + vb.len);
1484
49.8k
    if (result != NULL) {
1485
49.8k
        memcpy(PyBytes_AS_STRING(result), va.buf, va.len);
1486
49.8k
        memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len);
1487
49.8k
    }
1488
1489
61.4k
  done:
1490
61.4k
    if (va.len != -1)
1491
61.4k
        PyBuffer_Release(&va);
1492
61.4k
    if (vb.len != -1)
1493
61.4k
        PyBuffer_Release(&vb);
1494
61.4k
    return result;
1495
49.8k
}
1496
1497
static PyObject *
1498
bytes_repeat(PyObject *self, Py_ssize_t n)
1499
17
{
1500
17
    PyBytesObject *a = _PyBytes_CAST(self);
1501
17
    if (n < 0)
1502
0
        n = 0;
1503
    /* watch out for overflows:  the size can overflow int,
1504
     * and the # of bytes needed can overflow size_t
1505
     */
1506
17
    if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n) {
1507
0
        PyErr_SetString(PyExc_OverflowError,
1508
0
            "repeated bytes are too long");
1509
0
        return NULL;
1510
0
    }
1511
17
    Py_ssize_t size = Py_SIZE(a) * n;
1512
17
    if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) {
1513
0
        return Py_NewRef(a);
1514
0
    }
1515
17
    size_t nbytes = (size_t)size;
1516
17
    if (nbytes + PyBytesObject_SIZE <= nbytes) {
1517
0
        PyErr_SetString(PyExc_OverflowError,
1518
0
            "repeated bytes are too long");
1519
0
        return NULL;
1520
0
    }
1521
17
    PyBytesObject *op = PyObject_Malloc(PyBytesObject_SIZE + nbytes);
1522
17
    if (op == NULL) {
1523
0
        return PyErr_NoMemory();
1524
0
    }
1525
17
    _PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
1526
17
    set_ob_shash(op, -1);
1527
17
    op->ob_sval[size] = '\0';
1528
1529
17
    _PyBytes_Repeat(op->ob_sval, size, a->ob_sval, Py_SIZE(a));
1530
1531
17
    return (PyObject *) op;
1532
17
}
1533
1534
static int
1535
bytes_contains(PyObject *self, PyObject *arg)
1536
3.11k
{
1537
3.11k
    return _Py_bytes_contains(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), arg);
1538
3.11k
}
1539
1540
static PyObject *
1541
bytes_item(PyObject *self, Py_ssize_t i)
1542
0
{
1543
0
    PyBytesObject *a = _PyBytes_CAST(self);
1544
0
    if (i < 0 || i >= Py_SIZE(a)) {
1545
0
        PyErr_SetString(PyExc_IndexError, "index out of range");
1546
0
        return NULL;
1547
0
    }
1548
0
    return _PyLong_FromUnsignedChar((unsigned char)a->ob_sval[i]);
1549
0
}
1550
1551
static int
1552
bytes_compare_eq(PyBytesObject *a, PyBytesObject *b)
1553
448k
{
1554
448k
    int cmp;
1555
448k
    Py_ssize_t len;
1556
1557
448k
    len = Py_SIZE(a);
1558
448k
    if (Py_SIZE(b) != len)
1559
334k
        return 0;
1560
1561
113k
    if (a->ob_sval[0] != b->ob_sval[0])
1562
10.2k
        return 0;
1563
1564
103k
    cmp = memcmp(a->ob_sval, b->ob_sval, len);
1565
103k
    return (cmp == 0);
1566
113k
}
1567
1568
static PyObject*
1569
bytes_richcompare(PyObject *aa, PyObject *bb, int op)
1570
448k
{
1571
    /* Make sure both arguments are strings. */
1572
448k
    if (!(PyBytes_Check(aa) && PyBytes_Check(bb))) {
1573
0
        if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
1574
0
            if (PyUnicode_Check(aa) || PyUnicode_Check(bb)) {
1575
0
                if (PyErr_WarnEx(PyExc_BytesWarning,
1576
0
                                 "Comparison between bytes and string", 1))
1577
0
                    return NULL;
1578
0
            }
1579
0
            if (PyLong_Check(aa) || PyLong_Check(bb)) {
1580
0
                if (PyErr_WarnEx(PyExc_BytesWarning,
1581
0
                                 "Comparison between bytes and int", 1))
1582
0
                    return NULL;
1583
0
            }
1584
0
        }
1585
0
        Py_RETURN_NOTIMPLEMENTED;
1586
0
    }
1587
1588
448k
    PyBytesObject *a = _PyBytes_CAST(aa);
1589
448k
    PyBytesObject *b = _PyBytes_CAST(bb);
1590
448k
    if (a == b) {
1591
0
        switch (op) {
1592
0
        case Py_EQ:
1593
0
        case Py_LE:
1594
0
        case Py_GE:
1595
            /* a byte string is equal to itself */
1596
0
            Py_RETURN_TRUE;
1597
0
        case Py_NE:
1598
0
        case Py_LT:
1599
0
        case Py_GT:
1600
0
            Py_RETURN_FALSE;
1601
0
        default:
1602
0
            PyErr_BadArgument();
1603
0
            return NULL;
1604
0
        }
1605
0
    }
1606
448k
    else if (op == Py_EQ || op == Py_NE) {
1607
448k
        int eq = bytes_compare_eq(a, b);
1608
448k
        eq ^= (op == Py_NE);
1609
448k
        return PyBool_FromLong(eq);
1610
448k
    }
1611
146
    else {
1612
146
        Py_ssize_t len_a = Py_SIZE(a);
1613
146
        Py_ssize_t len_b = Py_SIZE(b);
1614
146
        Py_ssize_t min_len = Py_MIN(len_a, len_b);
1615
146
        int c;
1616
146
        if (min_len > 0) {
1617
146
            c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
1618
146
            if (c == 0)
1619
146
                c = memcmp(a->ob_sval, b->ob_sval, min_len);
1620
146
        }
1621
0
        else {
1622
0
            c = 0;
1623
0
        }
1624
146
        if (c != 0) {
1625
146
            Py_RETURN_RICHCOMPARE(c, 0, op);
1626
146
        }
1627
0
        Py_RETURN_RICHCOMPARE(len_a, len_b, op);
1628
0
    }
1629
448k
}
1630
1631
static Py_hash_t
1632
bytes_hash(PyObject *self)
1633
4.44M
{
1634
4.44M
    PyBytesObject *a = _PyBytes_CAST(self);
1635
4.44M
    Py_hash_t hash = get_ob_shash(a);
1636
4.44M
    if (hash == -1) {
1637
        /* Can't fail */
1638
167k
        hash = Py_HashBuffer(a->ob_sval, Py_SIZE(a));
1639
167k
        set_ob_shash(a, hash);
1640
167k
    }
1641
4.44M
    return hash;
1642
4.44M
}
1643
1644
static PyObject*
1645
bytes_subscript(PyObject *op, PyObject* item)
1646
5.41M
{
1647
5.41M
    PyBytesObject *self = _PyBytes_CAST(op);
1648
5.41M
    if (_PyIndex_Check(item)) {
1649
905k
        Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
1650
905k
        if (i == -1 && PyErr_Occurred())
1651
0
            return NULL;
1652
905k
        if (i < 0)
1653
0
            i += PyBytes_GET_SIZE(self);
1654
905k
        if (i < 0 || i >= PyBytes_GET_SIZE(self)) {
1655
62
            PyErr_SetString(PyExc_IndexError,
1656
62
                            "index out of range");
1657
62
            return NULL;
1658
62
        }
1659
905k
        return _PyLong_FromUnsignedChar((unsigned char)self->ob_sval[i]);
1660
905k
    }
1661
4.51M
    else if (PySlice_Check(item)) {
1662
4.51M
        Py_ssize_t start, stop, step, slicelength, i;
1663
4.51M
        size_t cur;
1664
4.51M
        const char* source_buf;
1665
4.51M
        char* result_buf;
1666
4.51M
        PyObject* result;
1667
1668
4.51M
        if (PySlice_Unpack(item, &start, &stop, &step) < 0) {
1669
0
            return NULL;
1670
0
        }
1671
4.51M
        slicelength = PySlice_AdjustIndices(PyBytes_GET_SIZE(self), &start,
1672
4.51M
                                            &stop, step);
1673
1674
4.51M
        if (slicelength <= 0) {
1675
4.20M
            return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
1676
4.20M
        }
1677
303k
        else if (start == 0 && step == 1 &&
1678
215k
                 slicelength == PyBytes_GET_SIZE(self) &&
1679
101k
                 PyBytes_CheckExact(self)) {
1680
101k
            return Py_NewRef(self);
1681
101k
        }
1682
201k
        else if (step == 1) {
1683
201k
            return PyBytes_FromStringAndSize(
1684
201k
                PyBytes_AS_STRING(self) + start,
1685
201k
                slicelength);
1686
201k
        }
1687
0
        else {
1688
0
            source_buf = PyBytes_AS_STRING(self);
1689
0
            result = PyBytes_FromStringAndSize(NULL, slicelength);
1690
0
            if (result == NULL)
1691
0
                return NULL;
1692
1693
0
            result_buf = PyBytes_AS_STRING(result);
1694
0
            for (cur = start, i = 0; i < slicelength;
1695
0
                 cur += step, i++) {
1696
0
                result_buf[i] = source_buf[cur];
1697
0
            }
1698
1699
0
            return result;
1700
0
        }
1701
4.51M
    }
1702
0
    else {
1703
0
        PyErr_Format(PyExc_TypeError,
1704
0
                     "byte indices must be integers or slices, not %.200s",
1705
0
                     Py_TYPE(item)->tp_name);
1706
0
        return NULL;
1707
0
    }
1708
5.41M
}
1709
1710
static int
1711
bytes_buffer_getbuffer(PyObject *op, Py_buffer *view, int flags)
1712
13.3M
{
1713
13.3M
    PyBytesObject *self = _PyBytes_CAST(op);
1714
13.3M
    return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self),
1715
13.3M
                             1, flags);
1716
13.3M
}
1717
1718
static PySequenceMethods bytes_as_sequence = {
1719
    bytes_length,       /*sq_length*/
1720
    bytes_concat,       /*sq_concat*/
1721
    bytes_repeat,       /*sq_repeat*/
1722
    bytes_item,         /*sq_item*/
1723
    0,                  /*sq_slice*/
1724
    0,                  /*sq_ass_item*/
1725
    0,                  /*sq_ass_slice*/
1726
    bytes_contains      /*sq_contains*/
1727
};
1728
1729
static PyMappingMethods bytes_as_mapping = {
1730
    bytes_length,
1731
    bytes_subscript,
1732
    0,
1733
};
1734
1735
static PyBufferProcs bytes_as_buffer = {
1736
    bytes_buffer_getbuffer,
1737
    NULL,
1738
};
1739
1740
1741
/*[clinic input]
1742
bytes.__bytes__
1743
Convert this value to exact type bytes.
1744
[clinic start generated code]*/
1745
1746
static PyObject *
1747
bytes___bytes___impl(PyBytesObject *self)
1748
/*[clinic end generated code: output=63a306a9bc0caac5 input=34ec5ddba98bd6bb]*/
1749
53.8k
{
1750
53.8k
    if (PyBytes_CheckExact(self)) {
1751
53.8k
        return Py_NewRef(self);
1752
53.8k
    }
1753
0
    else {
1754
0
        return PyBytes_FromStringAndSize(self->ob_sval, Py_SIZE(self));
1755
0
    }
1756
53.8k
}
1757
1758
1759
0
#define LEFTSTRIP 0
1760
0
#define RIGHTSTRIP 1
1761
0
#define BOTHSTRIP 2
1762
1763
/*[clinic input]
1764
bytes.split
1765
1766
    sep: object = None
1767
        The delimiter according which to split the bytes.
1768
        None (the default value) means split on ASCII whitespace characters
1769
        (space, tab, return, newline, formfeed, vertical tab).
1770
    maxsplit: Py_ssize_t = -1
1771
        Maximum number of splits to do.
1772
        -1 (the default value) means no limit.
1773
1774
Return a list of the sections in the bytes, using sep as the delimiter.
1775
[clinic start generated code]*/
1776
1777
static PyObject *
1778
bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit)
1779
/*[clinic end generated code: output=52126b5844c1d8ef input=8b809b39074abbfa]*/
1780
3.22M
{
1781
3.22M
    Py_ssize_t len = PyBytes_GET_SIZE(self), n;
1782
3.22M
    const char *s = PyBytes_AS_STRING(self), *sub;
1783
3.22M
    Py_buffer vsub;
1784
3.22M
    PyObject *list;
1785
1786
3.22M
    if (maxsplit < 0)
1787
3.22M
        maxsplit = PY_SSIZE_T_MAX;
1788
3.22M
    if (sep == Py_None)
1789
0
        return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
1790
3.22M
    if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
1791
0
        return NULL;
1792
3.22M
    sub = vsub.buf;
1793
3.22M
    n = vsub.len;
1794
1795
3.22M
    list = stringlib_split((PyObject*) self, s, len, sub, n, maxsplit);
1796
3.22M
    PyBuffer_Release(&vsub);
1797
3.22M
    return list;
1798
3.22M
}
1799
1800
/*[clinic input]
1801
@permit_long_docstring_body
1802
bytes.partition
1803
1804
    sep: Py_buffer
1805
    /
1806
1807
Partition the bytes into three parts using the given separator.
1808
1809
This will search for the separator sep in the bytes. If the separator is found,
1810
returns a 3-tuple containing the part before the separator, the separator
1811
itself, and the part after it.
1812
1813
If the separator is not found, returns a 3-tuple containing the original bytes
1814
object and two empty bytes objects.
1815
[clinic start generated code]*/
1816
1817
static PyObject *
1818
bytes_partition_impl(PyBytesObject *self, Py_buffer *sep)
1819
/*[clinic end generated code: output=f532b392a17ff695 input=31c55a0cebaf7722]*/
1820
0
{
1821
0
    return stringlib_partition(
1822
0
        (PyObject*) self,
1823
0
        PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
1824
0
        sep->obj, (const char *)sep->buf, sep->len
1825
0
        );
1826
0
}
1827
1828
/*[clinic input]
1829
@permit_long_docstring_body
1830
bytes.rpartition
1831
1832
    sep: Py_buffer
1833
    /
1834
1835
Partition the bytes into three parts using the given separator.
1836
1837
This will search for the separator sep in the bytes, starting at the end. If
1838
the separator is found, returns a 3-tuple containing the part before the
1839
separator, the separator itself, and the part after it.
1840
1841
If the separator is not found, returns a 3-tuple containing two empty bytes
1842
objects and the original bytes object.
1843
[clinic start generated code]*/
1844
1845
static PyObject *
1846
bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep)
1847
/*[clinic end generated code: output=191b114cbb028e50 input=9ea5a3ab0b02bf52]*/
1848
0
{
1849
0
    return stringlib_rpartition(
1850
0
        (PyObject*) self,
1851
0
        PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
1852
0
        sep->obj, (const char *)sep->buf, sep->len
1853
0
        );
1854
0
}
1855
1856
/*[clinic input]
1857
@permit_long_docstring_body
1858
bytes.rsplit = bytes.split
1859
1860
Return a list of the sections in the bytes, using sep as the delimiter.
1861
1862
Splitting is done starting at the end of the bytes and working to the front.
1863
[clinic start generated code]*/
1864
1865
static PyObject *
1866
bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit)
1867
/*[clinic end generated code: output=ba698d9ea01e1c8f input=55b6eaea1f3d7046]*/
1868
0
{
1869
0
    Py_ssize_t len = PyBytes_GET_SIZE(self), n;
1870
0
    const char *s = PyBytes_AS_STRING(self), *sub;
1871
0
    Py_buffer vsub;
1872
0
    PyObject *list;
1873
1874
0
    if (maxsplit < 0)
1875
0
        maxsplit = PY_SSIZE_T_MAX;
1876
0
    if (sep == Py_None)
1877
0
        return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
1878
0
    if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
1879
0
        return NULL;
1880
0
    sub = vsub.buf;
1881
0
    n = vsub.len;
1882
1883
0
    list = stringlib_rsplit((PyObject*) self, s, len, sub, n, maxsplit);
1884
0
    PyBuffer_Release(&vsub);
1885
0
    return list;
1886
0
}
1887
1888
1889
/*[clinic input]
1890
bytes.join
1891
1892
    iterable_of_bytes: object
1893
    /
1894
1895
Concatenate any number of bytes objects.
1896
1897
The bytes whose method is called is inserted in between each pair.
1898
1899
The result is returned as a new bytes object.
1900
1901
Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'.
1902
[clinic start generated code]*/
1903
1904
static PyObject *
1905
bytes_join_impl(PyBytesObject *self, PyObject *iterable_of_bytes)
1906
/*[clinic end generated code: output=0687abb94d7d438e input=7fe377b95bd549d2]*/
1907
8.70k
{
1908
8.70k
    return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
1909
8.70k
}
1910
1911
PyObject *
1912
PyBytes_Join(PyObject *sep, PyObject *iterable)
1913
39.6k
{
1914
39.6k
    if (sep == NULL) {
1915
0
        PyErr_BadInternalCall();
1916
0
        return NULL;
1917
0
    }
1918
39.6k
    if (!PyBytes_Check(sep)) {
1919
0
        PyErr_Format(PyExc_TypeError,
1920
0
                     "sep: expected bytes, got %T", sep);
1921
0
        return NULL;
1922
0
    }
1923
1924
39.6k
    return stringlib_bytes_join(sep, iterable);
1925
39.6k
}
1926
1927
/*[clinic input]
1928
@permit_long_summary
1929
@text_signature "($self, sub[, start[, end]], /)"
1930
bytes.find
1931
1932
    sub: object
1933
    start: slice_index(accept={int, NoneType}, c_default='0') = None
1934
         Optional start position. Default: start of the bytes.
1935
    end: slice_index(accept={int, NoneType}, c_default='PY_SSIZE_T_MAX') = None
1936
         Optional stop position. Default: end of the bytes.
1937
    /
1938
1939
Return the lowest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start,end].
1940
1941
Return -1 on failure.
1942
[clinic start generated code]*/
1943
1944
static PyObject *
1945
bytes_find_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
1946
                Py_ssize_t end)
1947
/*[clinic end generated code: output=d5961a1c77b472a1 input=47d0929adafc6b0b]*/
1948
0
{
1949
0
    return _Py_bytes_find(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
1950
0
                          sub, start, end);
1951
0
}
1952
1953
/*[clinic input]
1954
@permit_long_summary
1955
bytes.index = bytes.find
1956
1957
Return the lowest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start,end].
1958
1959
Raise ValueError if the subsection is not found.
1960
[clinic start generated code]*/
1961
1962
static PyObject *
1963
bytes_index_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
1964
                 Py_ssize_t end)
1965
/*[clinic end generated code: output=0da25cc74683ba42 input=1cb45ce71456a269]*/
1966
0
{
1967
0
    return _Py_bytes_index(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
1968
0
                           sub, start, end);
1969
0
}
1970
1971
/*[clinic input]
1972
@permit_long_summary
1973
bytes.rfind = bytes.find
1974
1975
Return the highest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start,end].
1976
1977
Return -1 on failure.
1978
[clinic start generated code]*/
1979
1980
static PyObject *
1981
bytes_rfind_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
1982
                 Py_ssize_t end)
1983
/*[clinic end generated code: output=51b60fa4ad011c09 input=c9473d714251f1ab]*/
1984
20.6k
{
1985
20.6k
    return _Py_bytes_rfind(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
1986
20.6k
                           sub, start, end);
1987
20.6k
}
1988
1989
/*[clinic input]
1990
@permit_long_summary
1991
bytes.rindex = bytes.find
1992
1993
Return the highest index in B where subsection 'sub' is found, such that 'sub' is contained within B[start,end].
1994
1995
Raise ValueError if the subsection is not found.
1996
[clinic start generated code]*/
1997
1998
static PyObject *
1999
bytes_rindex_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
2000
                  Py_ssize_t end)
2001
/*[clinic end generated code: output=42bf674e0a0aabf6 input=bb5f473c64610c43]*/
2002
0
{
2003
0
    return _Py_bytes_rindex(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
2004
0
                            sub, start, end);
2005
0
}
2006
2007
2008
Py_LOCAL_INLINE(PyObject *)
2009
do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj)
2010
0
{
2011
0
    Py_buffer vsep;
2012
0
    const char *s = PyBytes_AS_STRING(self);
2013
0
    Py_ssize_t len = PyBytes_GET_SIZE(self);
2014
0
    char *sep;
2015
0
    Py_ssize_t seplen;
2016
0
    Py_ssize_t i, j;
2017
2018
0
    if (PyObject_GetBuffer(sepobj, &vsep, PyBUF_SIMPLE) != 0)
2019
0
        return NULL;
2020
0
    sep = vsep.buf;
2021
0
    seplen = vsep.len;
2022
2023
0
    i = 0;
2024
0
    if (striptype != RIGHTSTRIP) {
2025
0
        while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) {
2026
0
            i++;
2027
0
        }
2028
0
    }
2029
2030
0
    j = len;
2031
0
    if (striptype != LEFTSTRIP) {
2032
0
        do {
2033
0
            j--;
2034
0
        } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen));
2035
0
        j++;
2036
0
    }
2037
2038
0
    PyBuffer_Release(&vsep);
2039
2040
0
    if (i == 0 && j == len && PyBytes_CheckExact(self)) {
2041
0
        return Py_NewRef(self);
2042
0
    }
2043
0
    else
2044
0
        return PyBytes_FromStringAndSize(s+i, j-i);
2045
0
}
2046
2047
2048
Py_LOCAL_INLINE(PyObject *)
2049
do_strip(PyBytesObject *self, int striptype)
2050
0
{
2051
0
    const char *s = PyBytes_AS_STRING(self);
2052
0
    Py_ssize_t len = PyBytes_GET_SIZE(self), i, j;
2053
2054
0
    i = 0;
2055
0
    if (striptype != RIGHTSTRIP) {
2056
0
        while (i < len && Py_ISSPACE(s[i])) {
2057
0
            i++;
2058
0
        }
2059
0
    }
2060
2061
0
    j = len;
2062
0
    if (striptype != LEFTSTRIP) {
2063
0
        do {
2064
0
            j--;
2065
0
        } while (j >= i && Py_ISSPACE(s[j]));
2066
0
        j++;
2067
0
    }
2068
2069
0
    if (i == 0 && j == len && PyBytes_CheckExact(self)) {
2070
0
        return Py_NewRef(self);
2071
0
    }
2072
0
    else
2073
0
        return PyBytes_FromStringAndSize(s+i, j-i);
2074
0
}
2075
2076
2077
Py_LOCAL_INLINE(PyObject *)
2078
do_argstrip(PyBytesObject *self, int striptype, PyObject *bytes)
2079
0
{
2080
0
    if (bytes != Py_None) {
2081
0
        return do_xstrip(self, striptype, bytes);
2082
0
    }
2083
0
    return do_strip(self, striptype);
2084
0
}
2085
2086
/*[clinic input]
2087
@permit_long_docstring_body
2088
bytes.strip
2089
2090
    bytes: object = None
2091
    /
2092
2093
Strip leading and trailing bytes contained in the argument.
2094
2095
If the argument is omitted or None, strip leading and trailing ASCII whitespace.
2096
[clinic start generated code]*/
2097
2098
static PyObject *
2099
bytes_strip_impl(PyBytesObject *self, PyObject *bytes)
2100
/*[clinic end generated code: output=c7c228d3bd104a1b input=71904cd278c0ee03]*/
2101
0
{
2102
0
    return do_argstrip(self, BOTHSTRIP, bytes);
2103
0
}
2104
2105
/*[clinic input]
2106
bytes.lstrip
2107
2108
    bytes: object = None
2109
    /
2110
2111
Strip leading bytes contained in the argument.
2112
2113
If the argument is omitted or None, strip leading  ASCII whitespace.
2114
[clinic start generated code]*/
2115
2116
static PyObject *
2117
bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes)
2118
/*[clinic end generated code: output=28602e586f524e82 input=9baff4398c3f6857]*/
2119
0
{
2120
0
    return do_argstrip(self, LEFTSTRIP, bytes);
2121
0
}
2122
2123
/*[clinic input]
2124
bytes.rstrip
2125
2126
    bytes: object = None
2127
    /
2128
2129
Strip trailing bytes contained in the argument.
2130
2131
If the argument is omitted or None, strip trailing ASCII whitespace.
2132
[clinic start generated code]*/
2133
2134
static PyObject *
2135
bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes)
2136
/*[clinic end generated code: output=547e3815c95447da input=b78af445c727e32b]*/
2137
0
{
2138
0
    return do_argstrip(self, RIGHTSTRIP, bytes);
2139
0
}
2140
2141
2142
/*[clinic input]
2143
@permit_long_summary
2144
bytes.count = bytes.find
2145
2146
Return the number of non-overlapping occurrences of subsection 'sub' in bytes B[start:end].
2147
[clinic start generated code]*/
2148
2149
static PyObject *
2150
bytes_count_impl(PyBytesObject *self, PyObject *sub, Py_ssize_t start,
2151
                 Py_ssize_t end)
2152
/*[clinic end generated code: output=9848140b9be17d0f input=bb2f136f83f0d30e]*/
2153
0
{
2154
0
    return _Py_bytes_count(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
2155
0
                           sub, start, end);
2156
0
}
2157
2158
2159
/*[clinic input]
2160
bytes.translate
2161
2162
    table: object
2163
        Translation table, which must be a bytes object of length 256.
2164
    /
2165
    delete as deletechars: object(c_default="NULL") = b''
2166
2167
Return a copy with each character mapped by the given translation table.
2168
2169
All characters occurring in the optional argument delete are removed.
2170
The remaining characters are mapped through the given translation table.
2171
[clinic start generated code]*/
2172
2173
static PyObject *
2174
bytes_translate_impl(PyBytesObject *self, PyObject *table,
2175
                     PyObject *deletechars)
2176
/*[clinic end generated code: output=43be3437f1956211 input=0ecdf159f654233c]*/
2177
0
{
2178
0
    const char *input;
2179
0
    char *output;
2180
0
    Py_buffer table_view = {NULL, NULL};
2181
0
    Py_buffer del_table_view = {NULL, NULL};
2182
0
    const char *table_chars;
2183
0
    Py_ssize_t i, c, changed = 0;
2184
0
    PyObject *input_obj = (PyObject*)self;
2185
0
    const char *output_start, *del_table_chars=NULL;
2186
0
    Py_ssize_t inlen, tablen, dellen = 0;
2187
0
    PyObject *result;
2188
0
    int trans_table[256];
2189
2190
0
    if (PyBytes_Check(table)) {
2191
0
        table_chars = PyBytes_AS_STRING(table);
2192
0
        tablen = PyBytes_GET_SIZE(table);
2193
0
    }
2194
0
    else if (table == Py_None) {
2195
0
        table_chars = NULL;
2196
0
        tablen = 256;
2197
0
    }
2198
0
    else {
2199
0
        if (PyObject_GetBuffer(table, &table_view, PyBUF_SIMPLE) != 0)
2200
0
            return NULL;
2201
0
        table_chars = table_view.buf;
2202
0
        tablen = table_view.len;
2203
0
    }
2204
2205
0
    if (tablen != 256) {
2206
0
        PyErr_SetString(PyExc_ValueError,
2207
0
          "translation table must be 256 characters long");
2208
0
        PyBuffer_Release(&table_view);
2209
0
        return NULL;
2210
0
    }
2211
2212
0
    if (deletechars != NULL) {
2213
0
        if (PyBytes_Check(deletechars)) {
2214
0
            del_table_chars = PyBytes_AS_STRING(deletechars);
2215
0
            dellen = PyBytes_GET_SIZE(deletechars);
2216
0
        }
2217
0
        else {
2218
0
            if (PyObject_GetBuffer(deletechars, &del_table_view, PyBUF_SIMPLE) != 0) {
2219
0
                PyBuffer_Release(&table_view);
2220
0
                return NULL;
2221
0
            }
2222
0
            del_table_chars = del_table_view.buf;
2223
0
            dellen = del_table_view.len;
2224
0
        }
2225
0
    }
2226
0
    else {
2227
0
        del_table_chars = NULL;
2228
0
        dellen = 0;
2229
0
    }
2230
2231
0
    inlen = PyBytes_GET_SIZE(input_obj);
2232
0
    result = PyBytes_FromStringAndSize((char *)NULL, inlen);
2233
0
    if (result == NULL) {
2234
0
        PyBuffer_Release(&del_table_view);
2235
0
        PyBuffer_Release(&table_view);
2236
0
        return NULL;
2237
0
    }
2238
0
    output_start = output = PyBytes_AS_STRING(result);
2239
0
    input = PyBytes_AS_STRING(input_obj);
2240
2241
0
    if (dellen == 0 && table_chars != NULL) {
2242
        /* If no deletions are required, use faster code */
2243
0
        for (i = inlen; --i >= 0; ) {
2244
0
            c = Py_CHARMASK(*input++);
2245
0
            if (Py_CHARMASK((*output++ = table_chars[c])) != c)
2246
0
                changed = 1;
2247
0
        }
2248
0
        if (!changed && PyBytes_CheckExact(input_obj)) {
2249
0
            Py_SETREF(result, Py_NewRef(input_obj));
2250
0
        }
2251
0
        PyBuffer_Release(&del_table_view);
2252
0
        PyBuffer_Release(&table_view);
2253
0
        return result;
2254
0
    }
2255
2256
0
    if (table_chars == NULL) {
2257
0
        for (i = 0; i < 256; i++)
2258
0
            trans_table[i] = Py_CHARMASK(i);
2259
0
    } else {
2260
0
        for (i = 0; i < 256; i++)
2261
0
            trans_table[i] = Py_CHARMASK(table_chars[i]);
2262
0
    }
2263
0
    PyBuffer_Release(&table_view);
2264
2265
0
    for (i = 0; i < dellen; i++)
2266
0
        trans_table[(int) Py_CHARMASK(del_table_chars[i])] = -1;
2267
0
    PyBuffer_Release(&del_table_view);
2268
2269
0
    for (i = inlen; --i >= 0; ) {
2270
0
        c = Py_CHARMASK(*input++);
2271
0
        if (trans_table[c] != -1)
2272
0
            if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
2273
0
                continue;
2274
0
        changed = 1;
2275
0
    }
2276
0
    if (!changed && PyBytes_CheckExact(input_obj)) {
2277
0
        Py_DECREF(result);
2278
0
        return Py_NewRef(input_obj);
2279
0
    }
2280
    /* Fix the size of the resulting byte string */
2281
0
    if (inlen > 0)
2282
0
        _PyBytes_Resize(&result, output - output_start);
2283
0
    return result;
2284
0
}
2285
2286
2287
/*[clinic input]
2288
2289
@permit_long_summary
2290
@permit_long_docstring_body
2291
@staticmethod
2292
bytes.maketrans
2293
2294
    frm: Py_buffer
2295
    to: Py_buffer
2296
    /
2297
2298
Return a translation table usable for the bytes or bytearray translate method.
2299
2300
The returned table will be one where each byte in frm is mapped to the byte at
2301
the same position in to.
2302
2303
The bytes objects frm and to must be of the same length.
2304
[clinic start generated code]*/
2305
2306
static PyObject *
2307
bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to)
2308
/*[clinic end generated code: output=a36f6399d4b77f6f input=a06b75f44d933fb3]*/
2309
28
{
2310
28
    return _Py_bytes_maketrans(frm, to);
2311
28
}
2312
2313
2314
/*[clinic input]
2315
@permit_long_docstring_body
2316
bytes.replace
2317
2318
    old: Py_buffer
2319
    new: Py_buffer
2320
    count: Py_ssize_t = -1
2321
        Maximum number of occurrences to replace.
2322
        -1 (the default value) means replace all occurrences.
2323
    /
2324
2325
Return a copy with all occurrences of substring old replaced by new.
2326
2327
If the optional argument count is given, only the first count occurrences are
2328
replaced.
2329
[clinic start generated code]*/
2330
2331
static PyObject *
2332
bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
2333
                   Py_ssize_t count)
2334
/*[clinic end generated code: output=994fa588b6b9c104 input=8b99a9ab32bc06a2]*/
2335
39.9k
{
2336
39.9k
    return stringlib_replace((PyObject *)self,
2337
39.9k
                             (const char *)old->buf, old->len,
2338
39.9k
                             (const char *)new->buf, new->len, count);
2339
39.9k
}
2340
2341
/** End DALKE **/
2342
2343
/*[clinic input]
2344
bytes.removeprefix as bytes_removeprefix
2345
2346
    prefix: Py_buffer
2347
    /
2348
2349
Return a bytes object with the given prefix string removed if present.
2350
2351
If the bytes starts with the prefix string, return bytes[len(prefix):].
2352
Otherwise, return a copy of the original bytes.
2353
[clinic start generated code]*/
2354
2355
static PyObject *
2356
bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix)
2357
/*[clinic end generated code: output=f006865331a06ab6 input=0c93bac817a8502c]*/
2358
0
{
2359
0
    const char *self_start = PyBytes_AS_STRING(self);
2360
0
    Py_ssize_t self_len = PyBytes_GET_SIZE(self);
2361
0
    const char *prefix_start = prefix->buf;
2362
0
    Py_ssize_t prefix_len = prefix->len;
2363
2364
0
    if (self_len >= prefix_len
2365
0
        && prefix_len > 0
2366
0
        && memcmp(self_start, prefix_start, prefix_len) == 0)
2367
0
    {
2368
0
        return PyBytes_FromStringAndSize(self_start + prefix_len,
2369
0
                                         self_len - prefix_len);
2370
0
    }
2371
2372
0
    if (PyBytes_CheckExact(self)) {
2373
0
        return Py_NewRef(self);
2374
0
    }
2375
2376
0
    return PyBytes_FromStringAndSize(self_start, self_len);
2377
0
}
2378
2379
/*[clinic input]
2380
bytes.removesuffix as bytes_removesuffix
2381
2382
    suffix: Py_buffer
2383
    /
2384
2385
Return a bytes object with the given suffix string removed if present.
2386
2387
If the bytes ends with the suffix string and that suffix is not empty,
2388
return bytes[:-len(prefix)].  Otherwise, return a copy of the original
2389
bytes.
2390
[clinic start generated code]*/
2391
2392
static PyObject *
2393
bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix)
2394
/*[clinic end generated code: output=d887d308e3242eeb input=9f4e1da8c637bbf1]*/
2395
0
{
2396
0
    const char *self_start = PyBytes_AS_STRING(self);
2397
0
    Py_ssize_t self_len = PyBytes_GET_SIZE(self);
2398
0
    const char *suffix_start = suffix->buf;
2399
0
    Py_ssize_t suffix_len = suffix->len;
2400
2401
0
    if (self_len >= suffix_len
2402
0
        && suffix_len > 0
2403
0
        && memcmp(self_start + self_len - suffix_len,
2404
0
                  suffix_start, suffix_len) == 0)
2405
0
    {
2406
0
        return PyBytes_FromStringAndSize(self_start,
2407
0
                                         self_len - suffix_len);
2408
0
    }
2409
2410
0
    if (PyBytes_CheckExact(self)) {
2411
0
        return Py_NewRef(self);
2412
0
    }
2413
2414
0
    return PyBytes_FromStringAndSize(self_start, self_len);
2415
0
}
2416
2417
/*[clinic input]
2418
@permit_long_summary
2419
@text_signature "($self, prefix[, start[, end]], /)"
2420
bytes.startswith
2421
2422
    prefix as subobj: object
2423
        A bytes or a tuple of bytes to try.
2424
    start: slice_index(accept={int, NoneType}, c_default='0') = None
2425
        Optional start position. Default: start of the bytes.
2426
    end: slice_index(accept={int, NoneType}, c_default='PY_SSIZE_T_MAX') = None
2427
        Optional stop position. Default: end of the bytes.
2428
    /
2429
2430
Return True if the bytes starts with the specified prefix, False otherwise.
2431
[clinic start generated code]*/
2432
2433
static PyObject *
2434
bytes_startswith_impl(PyBytesObject *self, PyObject *subobj,
2435
                      Py_ssize_t start, Py_ssize_t end)
2436
/*[clinic end generated code: output=b1e8da1cbd528e8c input=a14efd070f15be80]*/
2437
626k
{
2438
626k
    return _Py_bytes_startswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
2439
626k
                                subobj, start, end);
2440
626k
}
2441
2442
/*[clinic input]
2443
@permit_long_summary
2444
@text_signature "($self, suffix[, start[, end]], /)"
2445
bytes.endswith
2446
2447
    suffix as subobj: object
2448
        A bytes or a tuple of bytes to try.
2449
    start: slice_index(accept={int, NoneType}, c_default='0') = None
2450
         Optional start position. Default: start of the bytes.
2451
    end: slice_index(accept={int, NoneType}, c_default='PY_SSIZE_T_MAX') = None
2452
         Optional stop position. Default: end of the bytes.
2453
    /
2454
2455
Return True if the bytes ends with the specified suffix, False otherwise.
2456
[clinic start generated code]*/
2457
2458
static PyObject *
2459
bytes_endswith_impl(PyBytesObject *self, PyObject *subobj, Py_ssize_t start,
2460
                    Py_ssize_t end)
2461
/*[clinic end generated code: output=038b633111f3629d input=49e383eaaf292713]*/
2462
0
{
2463
0
    return _Py_bytes_endswith(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
2464
0
                              subobj, start, end);
2465
0
}
2466
2467
2468
/*[clinic input]
2469
bytes.decode
2470
2471
    encoding: str(c_default="NULL") = 'utf-8'
2472
        The encoding with which to decode the bytes.
2473
    errors: str(c_default="NULL") = 'strict'
2474
        The error handling scheme to use for the handling of decoding errors.
2475
        The default is 'strict' meaning that decoding errors raise a
2476
        UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
2477
        as well as any other name registered with codecs.register_error that
2478
        can handle UnicodeDecodeErrors.
2479
2480
Decode the bytes using the codec registered for encoding.
2481
[clinic start generated code]*/
2482
2483
static PyObject *
2484
bytes_decode_impl(PyBytesObject *self, const char *encoding,
2485
                  const char *errors)
2486
/*[clinic end generated code: output=5649a53dde27b314 input=958174769d2a40ca]*/
2487
4.39M
{
2488
4.39M
    return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
2489
4.39M
}
2490
2491
2492
/*[clinic input]
2493
@permit_long_docstring_body
2494
bytes.splitlines
2495
2496
    keepends: bool = False
2497
2498
Return a list of the lines in the bytes, breaking at line boundaries.
2499
2500
Line breaks are not included in the resulting list unless keepends is given and
2501
true.
2502
[clinic start generated code]*/
2503
2504
static PyObject *
2505
bytes_splitlines_impl(PyBytesObject *self, int keepends)
2506
/*[clinic end generated code: output=3484149a5d880ffb input=d17968d2a355fe55]*/
2507
0
{
2508
0
    return stringlib_splitlines(
2509
0
        (PyObject*) self, PyBytes_AS_STRING(self),
2510
0
        PyBytes_GET_SIZE(self), keepends
2511
0
        );
2512
0
}
2513
2514
/*[clinic input]
2515
@classmethod
2516
bytes.fromhex
2517
2518
    string: object
2519
    /
2520
2521
Create a bytes object from a string of hexadecimal numbers.
2522
2523
Spaces between two numbers are accepted.
2524
Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'.
2525
[clinic start generated code]*/
2526
2527
static PyObject *
2528
bytes_fromhex_impl(PyTypeObject *type, PyObject *string)
2529
/*[clinic end generated code: output=0973acc63661bb2e input=f37d98ed51088a21]*/
2530
39.2k
{
2531
39.2k
    PyObject *result = _PyBytes_FromHex(string, 0);
2532
39.2k
    if (type != &PyBytes_Type && result != NULL) {
2533
0
        Py_SETREF(result, PyObject_CallOneArg((PyObject *)type, result));
2534
0
    }
2535
39.2k
    return result;
2536
39.2k
}
2537
2538
PyObject*
2539
_PyBytes_FromHex(PyObject *string, int use_bytearray)
2540
39.2k
{
2541
39.2k
    Py_ssize_t hexlen, invalid_char;
2542
39.2k
    unsigned int top, bot;
2543
39.2k
    const Py_UCS1 *str, *start, *end;
2544
39.2k
    PyBytesWriter *writer = NULL;
2545
39.2k
    Py_buffer view;
2546
39.2k
    view.obj = NULL;
2547
2548
39.2k
    if (PyUnicode_Check(string)) {
2549
39.2k
        hexlen = PyUnicode_GET_LENGTH(string);
2550
2551
39.2k
        if (!PyUnicode_IS_ASCII(string)) {
2552
0
            const void *data = PyUnicode_DATA(string);
2553
0
            int kind = PyUnicode_KIND(string);
2554
0
            Py_ssize_t i;
2555
2556
            /* search for the first non-ASCII character */
2557
0
            for (i = 0; i < hexlen; i++) {
2558
0
                if (PyUnicode_READ(kind, data, i) >= 128)
2559
0
                    break;
2560
0
            }
2561
0
            invalid_char = i;
2562
0
            goto error;
2563
0
        }
2564
2565
39.2k
        assert(PyUnicode_KIND(string) == PyUnicode_1BYTE_KIND);
2566
39.2k
        str = PyUnicode_1BYTE_DATA(string);
2567
39.2k
    }
2568
0
    else if (PyObject_CheckBuffer(string)) {
2569
0
        if (PyObject_GetBuffer(string, &view, PyBUF_SIMPLE) != 0) {
2570
0
            return NULL;
2571
0
        }
2572
0
        hexlen = view.len;
2573
0
        str = view.buf;
2574
0
    }
2575
0
    else {
2576
0
        PyErr_Format(PyExc_TypeError,
2577
0
                     "fromhex() argument must be str or bytes-like, not %T",
2578
0
                     string);
2579
0
        return NULL;
2580
0
    }
2581
2582
    /* This overestimates if there are spaces */
2583
39.2k
    if (use_bytearray) {
2584
0
        writer = _PyBytesWriter_CreateByteArray(hexlen / 2);
2585
0
    }
2586
39.2k
    else {
2587
39.2k
        writer = PyBytesWriter_Create(hexlen / 2);
2588
39.2k
    }
2589
39.2k
    if (writer == NULL) {
2590
0
        goto release_buffer;
2591
0
    }
2592
39.2k
    char *buf = PyBytesWriter_GetData(writer);
2593
2594
39.2k
    start = str;
2595
39.2k
    end = str + hexlen;
2596
78.5k
    while (str < end) {
2597
        /* skip over spaces in the input */
2598
39.2k
        if (Py_ISSPACE(*str)) {
2599
0
            do {
2600
0
                str++;
2601
0
            } while (Py_ISSPACE(*str));
2602
0
            if (str >= end)
2603
0
                break;
2604
0
        }
2605
2606
39.2k
        top = _PyLong_DigitValue[*str];
2607
39.2k
        if (top >= 16) {
2608
0
            invalid_char = str - start;
2609
0
            goto error;
2610
0
        }
2611
39.2k
        str++;
2612
2613
39.2k
        bot = _PyLong_DigitValue[*str];
2614
39.2k
        if (bot >= 16) {
2615
            /* Check if we had a second digit */
2616
0
            if (str >= end){
2617
0
                invalid_char = -1;
2618
0
            } else {
2619
0
                invalid_char = str - start;
2620
0
            }
2621
0
            goto error;
2622
0
        }
2623
39.2k
        str++;
2624
2625
39.2k
        *buf++ = (unsigned char)((top << 4) + bot);
2626
39.2k
    }
2627
2628
39.2k
    if (view.obj != NULL) {
2629
0
       PyBuffer_Release(&view);
2630
0
    }
2631
39.2k
    return PyBytesWriter_FinishWithPointer(writer, buf);
2632
2633
0
  error:
2634
0
    if (invalid_char == -1) {
2635
0
        PyErr_SetString(PyExc_ValueError,
2636
0
                        "fromhex() arg must contain an even number of hexadecimal digits");
2637
0
    } else {
2638
0
        PyErr_Format(PyExc_ValueError,
2639
0
                     "non-hexadecimal number found in "
2640
0
                     "fromhex() arg at position %zd", invalid_char);
2641
0
    }
2642
0
    PyBytesWriter_Discard(writer);
2643
2644
0
  release_buffer:
2645
0
    if (view.obj != NULL) {
2646
0
        PyBuffer_Release(&view);
2647
0
    }
2648
0
    return NULL;
2649
0
}
2650
2651
/*[clinic input]
2652
bytes.hex
2653
2654
    sep: object = NULL
2655
        An optional single character or byte to separate hex bytes.
2656
    bytes_per_sep: int = 1
2657
        How many bytes between separators.  Positive values count from the
2658
        right, negative values count from the left.
2659
2660
Create a string of hexadecimal numbers from a bytes object.
2661
2662
Example:
2663
>>> value = b'\xb9\x01\xef'
2664
>>> value.hex()
2665
'b901ef'
2666
>>> value.hex(':')
2667
'b9:01:ef'
2668
>>> value.hex(':', 2)
2669
'b9:01ef'
2670
>>> value.hex(':', -2)
2671
'b901:ef'
2672
[clinic start generated code]*/
2673
2674
static PyObject *
2675
bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep)
2676
/*[clinic end generated code: output=1f134da504064139 input=1a21282b1f1ae595]*/
2677
0
{
2678
0
    const char *argbuf = PyBytes_AS_STRING(self);
2679
0
    Py_ssize_t arglen = PyBytes_GET_SIZE(self);
2680
0
    return _Py_strhex_with_sep(argbuf, arglen, sep, bytes_per_sep);
2681
0
}
2682
2683
static PyObject *
2684
bytes_getnewargs(PyObject *op, PyObject *Py_UNUSED(dummy))
2685
0
{
2686
0
    PyBytesObject *v = _PyBytes_CAST(op);
2687
0
    return Py_BuildValue("(y#)", v->ob_sval, Py_SIZE(v));
2688
0
}
2689
2690
2691
static PyMethodDef
2692
bytes_methods[] = {
2693
    {"__getnewargs__", bytes_getnewargs,  METH_NOARGS},
2694
    BYTES___BYTES___METHODDEF
2695
    {"capitalize", stringlib_capitalize, METH_NOARGS,
2696
     _Py_capitalize__doc__},
2697
    STRINGLIB_CENTER_METHODDEF
2698
    BYTES_COUNT_METHODDEF
2699
    BYTES_DECODE_METHODDEF
2700
    BYTES_ENDSWITH_METHODDEF
2701
    STRINGLIB_EXPANDTABS_METHODDEF
2702
    BYTES_FIND_METHODDEF
2703
    BYTES_FROMHEX_METHODDEF
2704
    BYTES_HEX_METHODDEF
2705
    BYTES_INDEX_METHODDEF
2706
    {"isalnum", stringlib_isalnum, METH_NOARGS,
2707
     _Py_isalnum__doc__},
2708
    {"isalpha", stringlib_isalpha, METH_NOARGS,
2709
     _Py_isalpha__doc__},
2710
    {"isascii", stringlib_isascii, METH_NOARGS,
2711
     _Py_isascii__doc__},
2712
    {"isdigit", stringlib_isdigit, METH_NOARGS,
2713
     _Py_isdigit__doc__},
2714
    {"islower", stringlib_islower, METH_NOARGS,
2715
     _Py_islower__doc__},
2716
    {"isspace", stringlib_isspace, METH_NOARGS,
2717
     _Py_isspace__doc__},
2718
    {"istitle", stringlib_istitle, METH_NOARGS,
2719
     _Py_istitle__doc__},
2720
    {"isupper", stringlib_isupper, METH_NOARGS,
2721
     _Py_isupper__doc__},
2722
    BYTES_JOIN_METHODDEF
2723
    STRINGLIB_LJUST_METHODDEF
2724
    {"lower", stringlib_lower, METH_NOARGS, _Py_lower__doc__},
2725
    BYTES_LSTRIP_METHODDEF
2726
    BYTES_MAKETRANS_METHODDEF
2727
    BYTES_PARTITION_METHODDEF
2728
    BYTES_REPLACE_METHODDEF
2729
    BYTES_REMOVEPREFIX_METHODDEF
2730
    BYTES_REMOVESUFFIX_METHODDEF
2731
    BYTES_RFIND_METHODDEF
2732
    BYTES_RINDEX_METHODDEF
2733
    STRINGLIB_RJUST_METHODDEF
2734
    BYTES_RPARTITION_METHODDEF
2735
    BYTES_RSPLIT_METHODDEF
2736
    BYTES_RSTRIP_METHODDEF
2737
    BYTES_SPLIT_METHODDEF
2738
    BYTES_SPLITLINES_METHODDEF
2739
    BYTES_STARTSWITH_METHODDEF
2740
    BYTES_STRIP_METHODDEF
2741
    {"swapcase", stringlib_swapcase, METH_NOARGS,
2742
     _Py_swapcase__doc__},
2743
    {"title", stringlib_title, METH_NOARGS, _Py_title__doc__},
2744
    BYTES_TRANSLATE_METHODDEF
2745
    {"upper", stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2746
    STRINGLIB_ZFILL_METHODDEF
2747
    {NULL,     NULL}                         /* sentinel */
2748
};
2749
2750
static PyObject *
2751
bytes_mod(PyObject *self, PyObject *arg)
2752
0
{
2753
0
    if (!PyBytes_Check(self)) {
2754
0
        Py_RETURN_NOTIMPLEMENTED;
2755
0
    }
2756
0
    return _PyBytes_FormatEx(PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
2757
0
                             arg, 0);
2758
0
}
2759
2760
static PyNumberMethods bytes_as_number = {
2761
    0,              /*nb_add*/
2762
    0,              /*nb_subtract*/
2763
    0,              /*nb_multiply*/
2764
    bytes_mod,      /*nb_remainder*/
2765
};
2766
2767
static PyObject *
2768
bytes_subtype_new(PyTypeObject *, PyObject *);
2769
2770
/*[clinic input]
2771
@classmethod
2772
bytes.__new__ as bytes_new
2773
2774
    source as x: object = NULL
2775
    encoding: str = NULL
2776
    errors: str = NULL
2777
2778
[clinic start generated code]*/
2779
2780
static PyObject *
2781
bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
2782
               const char *errors)
2783
/*[clinic end generated code: output=1e0c471be311a425 input=f0a966d19b7262b4]*/
2784
783k
{
2785
783k
    PyObject *bytes;
2786
783k
    PyObject *func;
2787
783k
    Py_ssize_t size;
2788
2789
783k
    if (x == NULL) {
2790
0
        if (encoding != NULL || errors != NULL) {
2791
0
            PyErr_SetString(PyExc_TypeError,
2792
0
                            encoding != NULL ?
2793
0
                            "encoding without a string argument" :
2794
0
                            "errors without a string argument");
2795
0
            return NULL;
2796
0
        }
2797
0
        bytes = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
2798
0
    }
2799
783k
    else if (encoding != NULL) {
2800
        /* Encode via the codec registry */
2801
198k
        if (!PyUnicode_Check(x)) {
2802
0
            PyErr_SetString(PyExc_TypeError,
2803
0
                            "encoding without a string argument");
2804
0
            return NULL;
2805
0
        }
2806
198k
        bytes = PyUnicode_AsEncodedString(x, encoding, errors);
2807
198k
    }
2808
585k
    else if (errors != NULL) {
2809
0
        PyErr_SetString(PyExc_TypeError,
2810
0
                        PyUnicode_Check(x) ?
2811
0
                        "string argument without an encoding" :
2812
0
                        "errors without a string argument");
2813
0
        return NULL;
2814
0
    }
2815
    /* We'd like to call PyObject_Bytes here, but we need to check for an
2816
       integer argument before deferring to PyBytes_FromObject, something
2817
       PyObject_Bytes doesn't do. */
2818
585k
    else if ((func = _PyObject_LookupSpecial(x, &_Py_ID(__bytes__))) != NULL) {
2819
53.8k
        bytes = _PyObject_CallNoArgs(func);
2820
53.8k
        Py_DECREF(func);
2821
53.8k
        if (bytes == NULL)
2822
0
            return NULL;
2823
53.8k
        if (!PyBytes_Check(bytes)) {
2824
0
            PyErr_Format(PyExc_TypeError,
2825
0
                         "%T.__bytes__() must return a bytes, not %T",
2826
0
                         x, bytes);
2827
0
            Py_DECREF(bytes);
2828
0
            return NULL;
2829
0
        }
2830
53.8k
    }
2831
531k
    else if (PyErr_Occurred())
2832
0
        return NULL;
2833
531k
    else if (PyUnicode_Check(x)) {
2834
0
        PyErr_SetString(PyExc_TypeError,
2835
0
                        "string argument without an encoding");
2836
0
        return NULL;
2837
0
    }
2838
    /* Is it an integer? */
2839
531k
    else if (_PyIndex_Check(x)) {
2840
0
        size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
2841
0
        if (size == -1 && PyErr_Occurred()) {
2842
0
            if (!PyErr_ExceptionMatches(PyExc_TypeError))
2843
0
                return NULL;
2844
0
            PyErr_Clear();  /* fall through */
2845
0
            bytes = PyBytes_FromObject(x);
2846
0
        }
2847
0
        else {
2848
0
            if (size < 0) {
2849
0
                PyErr_SetString(PyExc_ValueError, "negative count");
2850
0
                return NULL;
2851
0
            }
2852
0
            bytes = _PyBytes_FromSize(size, 1);
2853
0
        }
2854
0
    }
2855
531k
    else {
2856
531k
        bytes = PyBytes_FromObject(x);
2857
531k
    }
2858
2859
783k
    if (bytes != NULL && type != &PyBytes_Type) {
2860
0
        Py_SETREF(bytes, bytes_subtype_new(type, bytes));
2861
0
    }
2862
2863
783k
    return bytes;
2864
783k
}
2865
2866
static PyObject*
2867
_PyBytes_FromBuffer(PyObject *x)
2868
531k
{
2869
531k
    Py_buffer view;
2870
531k
    if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0)
2871
0
        return NULL;
2872
2873
531k
    PyBytesWriter *writer = PyBytesWriter_Create(view.len);
2874
531k
    if (writer == NULL) {
2875
0
        goto fail;
2876
0
    }
2877
2878
531k
    if (PyBuffer_ToContiguous(PyBytesWriter_GetData(writer),
2879
531k
                              &view, view.len, 'C') < 0) {
2880
0
        goto fail;
2881
0
    }
2882
2883
531k
    PyBuffer_Release(&view);
2884
531k
    return PyBytesWriter_Finish(writer);
2885
2886
0
fail:
2887
0
    PyBytesWriter_Discard(writer);
2888
0
    PyBuffer_Release(&view);
2889
0
    return NULL;
2890
531k
}
2891
2892
static PyObject*
2893
_PyBytes_FromList(PyObject *x)
2894
0
{
2895
0
    Py_ssize_t size = PyList_GET_SIZE(x);
2896
0
    PyBytesWriter *writer = PyBytesWriter_Create(size);
2897
0
    if (writer == NULL) {
2898
0
        return NULL;
2899
0
    }
2900
0
    char *str = PyBytesWriter_GetData(writer);
2901
0
    size = _PyBytesWriter_GetAllocated(writer);
2902
2903
0
    for (Py_ssize_t i = 0; i < PyList_GET_SIZE(x); i++) {
2904
0
        PyObject *item = PyList_GET_ITEM(x, i);
2905
0
        Py_INCREF(item);
2906
0
        Py_ssize_t value = PyNumber_AsSsize_t(item, NULL);
2907
0
        Py_DECREF(item);
2908
0
        if (value == -1 && PyErr_Occurred())
2909
0
            goto error;
2910
2911
0
        if (value < 0 || value >= 256) {
2912
0
            PyErr_SetString(PyExc_ValueError,
2913
0
                            "bytes must be in range(0, 256)");
2914
0
            goto error;
2915
0
        }
2916
2917
0
        if (i >= size) {
2918
0
            str = _PyBytesWriter_ResizeAndUpdatePointer(writer, size + 1, str);
2919
0
            if (str == NULL) {
2920
0
                goto error;
2921
0
            }
2922
0
            size = _PyBytesWriter_GetAllocated(writer);
2923
0
        }
2924
0
        *str++ = (char) value;
2925
0
    }
2926
0
    return PyBytesWriter_FinishWithPointer(writer, str);
2927
2928
0
error:
2929
0
    PyBytesWriter_Discard(writer);
2930
0
    return NULL;
2931
0
}
2932
2933
static PyObject*
2934
_PyBytes_FromTuple(PyObject *x)
2935
0
{
2936
0
    Py_ssize_t i, size = PyTuple_GET_SIZE(x);
2937
0
    Py_ssize_t value;
2938
0
    PyObject *item;
2939
2940
0
    PyBytesWriter *writer = PyBytesWriter_Create(size);
2941
0
    if (writer == NULL) {
2942
0
        return NULL;
2943
0
    }
2944
0
    char *str = PyBytesWriter_GetData(writer);
2945
2946
0
    for (i = 0; i < size; i++) {
2947
0
        item = PyTuple_GET_ITEM(x, i);
2948
0
        value = PyNumber_AsSsize_t(item, NULL);
2949
0
        if (value == -1 && PyErr_Occurred())
2950
0
            goto error;
2951
2952
0
        if (value < 0 || value >= 256) {
2953
0
            PyErr_SetString(PyExc_ValueError,
2954
0
                            "bytes must be in range(0, 256)");
2955
0
            goto error;
2956
0
        }
2957
0
        *str++ = (char) value;
2958
0
    }
2959
0
    return PyBytesWriter_Finish(writer);
2960
2961
0
  error:
2962
0
    PyBytesWriter_Discard(writer);
2963
0
    return NULL;
2964
0
}
2965
2966
static PyObject *
2967
_PyBytes_FromIterator(PyObject *it, PyObject *x)
2968
138
{
2969
138
    Py_ssize_t i, size;
2970
2971
    /* For iterator version, create a bytes object and resize as needed */
2972
138
    size = PyObject_LengthHint(x, 64);
2973
138
    if (size == -1 && PyErr_Occurred())
2974
0
        return NULL;
2975
2976
138
    PyBytesWriter *writer = PyBytesWriter_Create(size);
2977
138
    if (writer == NULL) {
2978
0
        return NULL;
2979
0
    }
2980
138
    char *str = PyBytesWriter_GetData(writer);
2981
138
    size = _PyBytesWriter_GetAllocated(writer);
2982
2983
    /* Run the iterator to exhaustion */
2984
1.06k
    for (i = 0; ; i++) {
2985
1.06k
        PyObject *item;
2986
1.06k
        Py_ssize_t value;
2987
2988
        /* Get the next item */
2989
1.06k
        item = PyIter_Next(it);
2990
1.06k
        if (item == NULL) {
2991
138
            if (PyErr_Occurred())
2992
0
                goto error;
2993
138
            break;
2994
138
        }
2995
2996
        /* Interpret it as an int (__index__) */
2997
924
        value = PyNumber_AsSsize_t(item, NULL);
2998
924
        Py_DECREF(item);
2999
924
        if (value == -1 && PyErr_Occurred())
3000
0
            goto error;
3001
3002
        /* Range check */
3003
924
        if (value < 0 || value >= 256) {
3004
0
            PyErr_SetString(PyExc_ValueError,
3005
0
                            "bytes must be in range(0, 256)");
3006
0
            goto error;
3007
0
        }
3008
3009
        /* Append the byte */
3010
924
        if (i >= size) {
3011
0
            str = _PyBytesWriter_ResizeAndUpdatePointer(writer, size + 1, str);
3012
0
            if (str == NULL) {
3013
0
                goto error;
3014
0
            }
3015
0
            size = _PyBytesWriter_GetAllocated(writer);
3016
0
        }
3017
924
        *str++ = (char) value;
3018
924
    }
3019
138
    return PyBytesWriter_FinishWithPointer(writer, str);
3020
3021
0
  error:
3022
0
    PyBytesWriter_Discard(writer);
3023
0
    return NULL;
3024
138
}
3025
3026
PyObject *
3027
PyBytes_FromObject(PyObject *x)
3028
531k
{
3029
531k
    PyObject *it, *result;
3030
3031
531k
    if (x == NULL) {
3032
0
        PyErr_BadInternalCall();
3033
0
        return NULL;
3034
0
    }
3035
3036
531k
    if (PyBytes_CheckExact(x)) {
3037
0
        return Py_NewRef(x);
3038
0
    }
3039
3040
    /* Use the modern buffer interface */
3041
531k
    if (PyObject_CheckBuffer(x))
3042
531k
        return _PyBytes_FromBuffer(x);
3043
3044
138
    if (PyList_CheckExact(x))
3045
0
        return _PyBytes_FromList(x);
3046
3047
138
    if (PyTuple_CheckExact(x))
3048
0
        return _PyBytes_FromTuple(x);
3049
3050
138
    if (!PyUnicode_Check(x)) {
3051
138
        it = PyObject_GetIter(x);
3052
138
        if (it != NULL) {
3053
138
            result = _PyBytes_FromIterator(it, x);
3054
138
            Py_DECREF(it);
3055
138
            return result;
3056
138
        }
3057
0
        if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
3058
0
            return NULL;
3059
0
        }
3060
0
    }
3061
3062
0
    PyErr_Format(PyExc_TypeError,
3063
0
                 "cannot convert '%.200s' object to bytes",
3064
0
                 Py_TYPE(x)->tp_name);
3065
0
    return NULL;
3066
138
}
3067
3068
/* This allocator is needed for subclasses don't want to use __new__.
3069
 * See https://github.com/python/cpython/issues/91020#issuecomment-1096793239
3070
 *
3071
 * This allocator will be removed when ob_shash is removed.
3072
 */
3073
static PyObject *
3074
bytes_alloc(PyTypeObject *self, Py_ssize_t nitems)
3075
0
{
3076
0
    PyBytesObject *obj = (PyBytesObject*)PyType_GenericAlloc(self, nitems);
3077
0
    if (obj == NULL) {
3078
0
        return NULL;
3079
0
    }
3080
0
    set_ob_shash(obj, -1);
3081
0
    return (PyObject*)obj;
3082
0
}
3083
3084
static PyObject *
3085
bytes_subtype_new(PyTypeObject *type, PyObject *tmp)
3086
0
{
3087
0
    PyObject *pnew;
3088
0
    Py_ssize_t n;
3089
3090
0
    assert(PyType_IsSubtype(type, &PyBytes_Type));
3091
0
    assert(PyBytes_Check(tmp));
3092
0
    n = PyBytes_GET_SIZE(tmp);
3093
0
    pnew = type->tp_alloc(type, n);
3094
0
    if (pnew != NULL) {
3095
0
        memcpy(PyBytes_AS_STRING(pnew),
3096
0
                  PyBytes_AS_STRING(tmp), n+1);
3097
0
        set_ob_shash((PyBytesObject *)pnew,
3098
0
            get_ob_shash((PyBytesObject *)tmp));
3099
0
    }
3100
0
    return pnew;
3101
0
}
3102
3103
PyDoc_STRVAR(bytes_doc,
3104
"bytes(iterable_of_ints) -> bytes\n\
3105
bytes(string, encoding[, errors]) -> bytes\n\
3106
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer\n\
3107
bytes(int) -> bytes object of size given by the parameter initialized with null bytes\n\
3108
bytes() -> empty bytes object\n\
3109
\n\
3110
Construct an immutable array of bytes from:\n\
3111
  - an iterable yielding integers in range(256)\n\
3112
  - a text string encoded using the specified encoding\n\
3113
  - any object implementing the buffer API.\n\
3114
  - an integer");
3115
3116
static PyObject *bytes_iter(PyObject *seq);
3117
3118
PyTypeObject PyBytes_Type = {
3119
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
3120
    "bytes",
3121
    PyBytesObject_SIZE,
3122
    sizeof(char),
3123
    0,                                          /* tp_dealloc */
3124
    0,                                          /* tp_vectorcall_offset */
3125
    0,                                          /* tp_getattr */
3126
    0,                                          /* tp_setattr */
3127
    0,                                          /* tp_as_async */
3128
    bytes_repr,                                 /* tp_repr */
3129
    &bytes_as_number,                           /* tp_as_number */
3130
    &bytes_as_sequence,                         /* tp_as_sequence */
3131
    &bytes_as_mapping,                          /* tp_as_mapping */
3132
    bytes_hash,                                 /* tp_hash */
3133
    0,                                          /* tp_call */
3134
    bytes_str,                                  /* tp_str */
3135
    PyObject_GenericGetAttr,                    /* tp_getattro */
3136
    0,                                          /* tp_setattro */
3137
    &bytes_as_buffer,                           /* tp_as_buffer */
3138
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
3139
        Py_TPFLAGS_BYTES_SUBCLASS |
3140
        _Py_TPFLAGS_MATCH_SELF,               /* tp_flags */
3141
    bytes_doc,                                  /* tp_doc */
3142
    0,                                          /* tp_traverse */
3143
    0,                                          /* tp_clear */
3144
    bytes_richcompare,                          /* tp_richcompare */
3145
    0,                                          /* tp_weaklistoffset */
3146
    bytes_iter,                                 /* tp_iter */
3147
    0,                                          /* tp_iternext */
3148
    bytes_methods,                              /* tp_methods */
3149
    0,                                          /* tp_members */
3150
    0,                                          /* tp_getset */
3151
    0,                                          /* tp_base */
3152
    0,                                          /* tp_dict */
3153
    0,                                          /* tp_descr_get */
3154
    0,                                          /* tp_descr_set */
3155
    0,                                          /* tp_dictoffset */
3156
    0,                                          /* tp_init */
3157
    bytes_alloc,                                /* tp_alloc */
3158
    bytes_new,                                  /* tp_new */
3159
    PyObject_Free,                              /* tp_free */
3160
    .tp_version_tag = _Py_TYPE_VERSION_BYTES,
3161
};
3162
3163
void
3164
PyBytes_Concat(PyObject **pv, PyObject *w)
3165
2.88k
{
3166
2.88k
    assert(pv != NULL);
3167
2.88k
    if (*pv == NULL)
3168
0
        return;
3169
2.88k
    if (w == NULL) {
3170
0
        Py_CLEAR(*pv);
3171
0
        return;
3172
0
    }
3173
3174
2.88k
    if (Py_REFCNT(*pv) == 1 && PyBytes_CheckExact(*pv)) {
3175
        /* Only one reference, so we can resize in place */
3176
554
        Py_ssize_t oldsize;
3177
554
        Py_buffer wb;
3178
3179
554
        if (PyObject_GetBuffer(w, &wb, PyBUF_SIMPLE) != 0) {
3180
0
            PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
3181
0
                         Py_TYPE(w)->tp_name, Py_TYPE(*pv)->tp_name);
3182
0
            Py_CLEAR(*pv);
3183
0
            return;
3184
0
        }
3185
3186
554
        oldsize = PyBytes_GET_SIZE(*pv);
3187
554
        if (oldsize > PY_SSIZE_T_MAX - wb.len) {
3188
0
            PyErr_NoMemory();
3189
0
            goto error;
3190
0
        }
3191
554
        if (_PyBytes_Resize(pv, oldsize + wb.len) < 0)
3192
0
            goto error;
3193
3194
554
        memcpy(PyBytes_AS_STRING(*pv) + oldsize, wb.buf, wb.len);
3195
554
        PyBuffer_Release(&wb);
3196
554
        return;
3197
3198
0
      error:
3199
0
        PyBuffer_Release(&wb);
3200
0
        Py_CLEAR(*pv);
3201
0
        return;
3202
554
    }
3203
3204
2.33k
    else {
3205
        /* Multiple references, need to create new object */
3206
2.33k
        PyObject *v;
3207
2.33k
        v = bytes_concat(*pv, w);
3208
2.33k
        Py_SETREF(*pv, v);
3209
2.33k
    }
3210
2.88k
}
3211
3212
void
3213
PyBytes_ConcatAndDel(PyObject **pv, PyObject *w)
3214
0
{
3215
0
    PyBytes_Concat(pv, w);
3216
0
    Py_XDECREF(w);
3217
0
}
3218
3219
3220
/* The following function breaks the notion that bytes are immutable:
3221
   it changes the size of a bytes object.  You can think of it
3222
   as creating a new bytes object and destroying the old one, only
3223
   more efficiently.
3224
   Note that if there's not enough memory to resize the bytes object, the
3225
   original bytes object at *pv is deallocated, *pv is set to NULL, an "out of
3226
   memory" exception is set, and -1 is returned.  Else (on success) 0 is
3227
   returned, and the value in *pv may or may not be the same as on input.
3228
   As always, an extra byte is allocated for a trailing \0 byte (newsize
3229
   does *not* include that), and a trailing \0 byte is stored.
3230
*/
3231
3232
int
3233
_PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
3234
352k
{
3235
352k
    PyObject *v;
3236
352k
    PyBytesObject *sv;
3237
352k
    v = *pv;
3238
352k
    if (!PyBytes_Check(v) || newsize < 0) {
3239
0
        *pv = 0;
3240
0
        Py_DECREF(v);
3241
0
        PyErr_BadInternalCall();
3242
0
        return -1;
3243
0
    }
3244
352k
    Py_ssize_t oldsize = PyBytes_GET_SIZE(v);
3245
352k
    if (oldsize == newsize) {
3246
        /* return early if newsize equals to v->ob_size */
3247
234
        return 0;
3248
234
    }
3249
352k
    if (oldsize == 0) {
3250
0
        *pv = _PyBytes_FromSize(newsize, 0);
3251
0
        Py_DECREF(v);
3252
0
        return (*pv == NULL) ? -1 : 0;
3253
0
    }
3254
352k
    if (newsize == 0) {
3255
5.86k
        *pv = bytes_get_empty();
3256
5.86k
        Py_DECREF(v);
3257
5.86k
        return 0;
3258
5.86k
    }
3259
346k
    if (Py_REFCNT(v) != 1) {
3260
0
        if (oldsize < newsize) {
3261
0
            *pv = _PyBytes_FromSize(newsize, 0);
3262
0
            if (*pv) {
3263
0
                memcpy(PyBytes_AS_STRING(*pv), PyBytes_AS_STRING(v), oldsize);
3264
0
            }
3265
0
        }
3266
0
        else {
3267
0
            *pv = PyBytes_FromStringAndSize(PyBytes_AS_STRING(v), newsize);
3268
0
        }
3269
0
        Py_DECREF(v);
3270
0
        return (*pv == NULL) ? -1 : 0;
3271
0
    }
3272
3273
#ifdef Py_TRACE_REFS
3274
    _Py_ForgetReference(v);
3275
#endif
3276
346k
    _PyReftracerTrack(v, PyRefTracer_DESTROY);
3277
346k
    *pv = (PyObject *)
3278
346k
        PyObject_Realloc(v, PyBytesObject_SIZE + newsize);
3279
346k
    if (*pv == NULL) {
3280
#ifdef Py_REF_DEBUG
3281
        _Py_DecRefTotal(_PyThreadState_GET());
3282
#endif
3283
0
        PyObject_Free(v);
3284
0
        PyErr_NoMemory();
3285
0
        return -1;
3286
0
    }
3287
346k
    _Py_NewReferenceNoTotal(*pv);
3288
346k
    sv = (PyBytesObject *) *pv;
3289
346k
    Py_SET_SIZE(sv, newsize);
3290
346k
    sv->ob_sval[newsize] = '\0';
3291
346k
    set_ob_shash(sv, -1);          /* invalidate cached hash value */
3292
346k
    return 0;
3293
346k
}
3294
3295
3296
/*********************** Bytes Iterator ****************************/
3297
3298
typedef struct {
3299
    PyObject_HEAD
3300
    Py_ssize_t it_index;
3301
    PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */
3302
} striterobject;
3303
3304
1.75k
#define _striterobject_CAST(op)  ((striterobject *)(op))
3305
3306
static void
3307
striter_dealloc(PyObject *op)
3308
52
{
3309
52
    striterobject *it = _striterobject_CAST(op);
3310
52
    _PyObject_GC_UNTRACK(it);
3311
52
    Py_XDECREF(it->it_seq);
3312
52
    PyObject_GC_Del(it);
3313
52
}
3314
3315
static int
3316
striter_traverse(PyObject *op, visitproc visit, void *arg)
3317
0
{
3318
0
    striterobject *it = _striterobject_CAST(op);
3319
0
    Py_VISIT(it->it_seq);
3320
0
    return 0;
3321
0
}
3322
3323
static PyObject *
3324
striter_next(PyObject *op)
3325
1.70k
{
3326
1.70k
    striterobject *it = _striterobject_CAST(op);
3327
1.70k
    PyBytesObject *seq;
3328
3329
1.70k
    assert(it != NULL);
3330
1.70k
    seq = it->it_seq;
3331
1.70k
    if (seq == NULL)
3332
0
        return NULL;
3333
1.70k
    assert(PyBytes_Check(seq));
3334
3335
1.70k
    if (it->it_index < PyBytes_GET_SIZE(seq)) {
3336
1.66k
        return _PyLong_FromUnsignedChar(
3337
1.66k
            (unsigned char)seq->ob_sval[it->it_index++]);
3338
1.66k
    }
3339
3340
36
    it->it_seq = NULL;
3341
36
    Py_DECREF(seq);
3342
36
    return NULL;
3343
1.70k
}
3344
3345
static PyObject *
3346
striter_len(PyObject *op, PyObject *Py_UNUSED(ignored))
3347
0
{
3348
0
    striterobject *it = _striterobject_CAST(op);
3349
0
    Py_ssize_t len = 0;
3350
0
    if (it->it_seq)
3351
0
        len = PyBytes_GET_SIZE(it->it_seq) - it->it_index;
3352
0
    return PyLong_FromSsize_t(len);
3353
0
}
3354
3355
PyDoc_STRVAR(length_hint_doc,
3356
             "Private method returning an estimate of len(list(it)).");
3357
3358
static PyObject *
3359
striter_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
3360
0
{
3361
0
    PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter));
3362
3363
    /* _PyEval_GetBuiltin can invoke arbitrary code,
3364
     * call must be before access of iterator pointers.
3365
     * see issue #101765 */
3366
0
    striterobject *it = _striterobject_CAST(op);
3367
0
    if (it->it_seq != NULL) {
3368
0
        return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index);
3369
0
    } else {
3370
0
        return Py_BuildValue("N(())", iter);
3371
0
    }
3372
0
}
3373
3374
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3375
3376
static PyObject *
3377
striter_setstate(PyObject *op, PyObject *state)
3378
0
{
3379
0
    Py_ssize_t index = PyLong_AsSsize_t(state);
3380
0
    if (index == -1 && PyErr_Occurred())
3381
0
        return NULL;
3382
0
    striterobject *it = _striterobject_CAST(op);
3383
0
    if (it->it_seq != NULL) {
3384
0
        if (index < 0)
3385
0
            index = 0;
3386
0
        else if (index > PyBytes_GET_SIZE(it->it_seq))
3387
0
            index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */
3388
0
        it->it_index = index;
3389
0
    }
3390
0
    Py_RETURN_NONE;
3391
0
}
3392
3393
PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3394
3395
static PyMethodDef striter_methods[] = {
3396
    {"__length_hint__", striter_len, METH_NOARGS, length_hint_doc},
3397
    {"__reduce__",      striter_reduce, METH_NOARGS, reduce_doc},
3398
    {"__setstate__",    striter_setstate, METH_O, setstate_doc},
3399
    {NULL,              NULL}           /* sentinel */
3400
};
3401
3402
PyTypeObject PyBytesIter_Type = {
3403
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
3404
    "bytes_iterator",                           /* tp_name */
3405
    sizeof(striterobject),                      /* tp_basicsize */
3406
    0,                                          /* tp_itemsize */
3407
    /* methods */
3408
    striter_dealloc,                            /* tp_dealloc */
3409
    0,                                          /* tp_vectorcall_offset */
3410
    0,                                          /* tp_getattr */
3411
    0,                                          /* tp_setattr */
3412
    0,                                          /* tp_as_async */
3413
    0,                                          /* tp_repr */
3414
    0,                                          /* tp_as_number */
3415
    0,                                          /* tp_as_sequence */
3416
    0,                                          /* tp_as_mapping */
3417
    0,                                          /* tp_hash */
3418
    0,                                          /* tp_call */
3419
    0,                                          /* tp_str */
3420
    PyObject_GenericGetAttr,                    /* tp_getattro */
3421
    0,                                          /* tp_setattro */
3422
    0,                                          /* tp_as_buffer */
3423
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3424
    0,                                          /* tp_doc */
3425
    striter_traverse,                           /* tp_traverse */
3426
    0,                                          /* tp_clear */
3427
    0,                                          /* tp_richcompare */
3428
    0,                                          /* tp_weaklistoffset */
3429
    PyObject_SelfIter,                          /* tp_iter */
3430
    striter_next,                               /* tp_iternext */
3431
    striter_methods,                            /* tp_methods */
3432
    0,
3433
};
3434
3435
static PyObject *
3436
bytes_iter(PyObject *seq)
3437
52
{
3438
52
    striterobject *it;
3439
3440
52
    if (!PyBytes_Check(seq)) {
3441
0
        PyErr_BadInternalCall();
3442
0
        return NULL;
3443
0
    }
3444
52
    it = PyObject_GC_New(striterobject, &PyBytesIter_Type);
3445
52
    if (it == NULL)
3446
0
        return NULL;
3447
52
    it->it_index = 0;
3448
52
    it->it_seq = (PyBytesObject *)Py_NewRef(seq);
3449
52
    _PyObject_GC_TRACK(it);
3450
52
    return (PyObject *)it;
3451
52
}
3452
3453
3454
void
3455
_PyBytes_Repeat(char* dest, Py_ssize_t len_dest,
3456
    const char* src, Py_ssize_t len_src)
3457
2.41k
{
3458
2.41k
    if (len_dest == 0) {
3459
0
        return;
3460
0
    }
3461
2.41k
    if (len_src == 1) {
3462
17
        memset(dest, src[0], len_dest);
3463
17
    }
3464
2.39k
    else {
3465
2.39k
        if (src != dest) {
3466
2.39k
            memcpy(dest, src, len_src);
3467
2.39k
        }
3468
2.39k
        Py_ssize_t copied = len_src;
3469
5.09k
        while (copied < len_dest) {
3470
2.69k
            Py_ssize_t bytes_to_copy = Py_MIN(copied, len_dest - copied);
3471
2.69k
            memcpy(dest + copied, dest, bytes_to_copy);
3472
2.69k
            copied += bytes_to_copy;
3473
2.69k
        }
3474
2.39k
    }
3475
2.41k
}
3476
3477
3478
// --- PyBytesWriter API -----------------------------------------------------
3479
3480
static inline char*
3481
byteswriter_data(PyBytesWriter *writer)
3482
13.8M
{
3483
13.8M
    return _PyBytesWriter_GetData(writer);
3484
13.8M
}
3485
3486
3487
static inline Py_ssize_t
3488
byteswriter_allocated(PyBytesWriter *writer)
3489
13.7M
{
3490
13.7M
    if (writer->obj == NULL) {
3491
13.4M
        return sizeof(writer->small_buffer);
3492
13.4M
    }
3493
309k
    else if (writer->use_bytearray) {
3494
0
        return PyByteArray_GET_SIZE(writer->obj);
3495
0
    }
3496
309k
    else {
3497
309k
        return PyBytes_GET_SIZE(writer->obj);
3498
309k
    }
3499
13.7M
}
3500
3501
3502
#ifdef MS_WINDOWS
3503
   /* On Windows, overallocate by 50% is the best factor */
3504
#  define OVERALLOCATE_FACTOR 2
3505
#else
3506
   /* On Linux, overallocate by 25% is the best factor */
3507
0
#  define OVERALLOCATE_FACTOR 4
3508
#endif
3509
3510
static inline int
3511
byteswriter_resize(PyBytesWriter *writer, Py_ssize_t size, int resize)
3512
7.30M
{
3513
7.30M
    assert(size >= 0);
3514
3515
7.30M
    Py_ssize_t old_allocated = byteswriter_allocated(writer);
3516
7.30M
    if (size <= old_allocated) {
3517
6.93M
        return 0;
3518
6.93M
    }
3519
3520
371k
    if (resize & writer->overallocate) {
3521
0
        if (size <= (PY_SSIZE_T_MAX - size / OVERALLOCATE_FACTOR)) {
3522
0
            size += size / OVERALLOCATE_FACTOR;
3523
0
        }
3524
0
    }
3525
3526
371k
    if (writer->obj != NULL) {
3527
0
        if (writer->use_bytearray) {
3528
0
            if (PyByteArray_Resize(writer->obj, size)) {
3529
0
                return -1;
3530
0
            }
3531
0
        }
3532
0
        else {
3533
0
            if (_PyBytes_Resize(&writer->obj, size)) {
3534
0
                return -1;
3535
0
            }
3536
0
        }
3537
0
        assert(writer->obj != NULL);
3538
0
    }
3539
371k
    else if (writer->use_bytearray) {
3540
0
        writer->obj = PyByteArray_FromStringAndSize(NULL, size);
3541
0
        if (writer->obj == NULL) {
3542
0
            return -1;
3543
0
        }
3544
0
        if (resize) {
3545
0
            assert((size_t)size > sizeof(writer->small_buffer));
3546
0
            memcpy(PyByteArray_AS_STRING(writer->obj),
3547
0
                   writer->small_buffer,
3548
0
                   sizeof(writer->small_buffer));
3549
0
        }
3550
0
    }
3551
371k
    else {
3552
371k
        writer->obj = PyBytes_FromStringAndSize(NULL, size);
3553
371k
        if (writer->obj == NULL) {
3554
0
            return -1;
3555
0
        }
3556
371k
        if (resize) {
3557
0
            assert((size_t)size > sizeof(writer->small_buffer));
3558
0
            memcpy(PyBytes_AS_STRING(writer->obj),
3559
0
                   writer->small_buffer,
3560
0
                   sizeof(writer->small_buffer));
3561
0
        }
3562
371k
    }
3563
3564
#ifdef Py_DEBUG
3565
    Py_ssize_t allocated = byteswriter_allocated(writer);
3566
    if (resize && allocated > old_allocated) {
3567
        memset(byteswriter_data(writer) + old_allocated, 0xff,
3568
               allocated - old_allocated);
3569
    }
3570
#endif
3571
3572
371k
    return 0;
3573
371k
}
3574
3575
3576
static PyBytesWriter*
3577
byteswriter_create(Py_ssize_t size, int use_bytearray)
3578
7.31M
{
3579
7.31M
    if (size < 0) {
3580
0
        PyErr_SetString(PyExc_ValueError, "size must be >= 0");
3581
0
        return NULL;
3582
0
    }
3583
3584
7.31M
    PyBytesWriter *writer = _Py_FREELIST_POP_MEM(bytes_writers);
3585
7.31M
    if (writer == NULL) {
3586
16
        writer = (PyBytesWriter *)PyMem_Malloc(sizeof(PyBytesWriter));
3587
16
        if (writer == NULL) {
3588
0
            PyErr_NoMemory();
3589
0
            return NULL;
3590
0
        }
3591
16
    }
3592
7.31M
    writer->obj = NULL;
3593
7.31M
    writer->size = 0;
3594
7.31M
    writer->use_bytearray = use_bytearray;
3595
7.31M
    writer->overallocate = !use_bytearray;
3596
3597
7.31M
    if (size >= 1) {
3598
7.30M
        if (byteswriter_resize(writer, size, 0) < 0) {
3599
0
            PyBytesWriter_Discard(writer);
3600
0
            return NULL;
3601
0
        }
3602
7.30M
        writer->size = size;
3603
7.30M
    }
3604
#ifdef Py_DEBUG
3605
    memset(byteswriter_data(writer), 0xff, byteswriter_allocated(writer));
3606
#endif
3607
7.31M
    return writer;
3608
7.31M
}
3609
3610
PyBytesWriter*
3611
PyBytesWriter_Create(Py_ssize_t size)
3612
7.31M
{
3613
7.31M
    return byteswriter_create(size, 0);
3614
7.31M
}
3615
3616
PyBytesWriter*
3617
_PyBytesWriter_CreateByteArray(Py_ssize_t size)
3618
0
{
3619
0
    return byteswriter_create(size, 1);
3620
0
}
3621
3622
3623
void
3624
PyBytesWriter_Discard(PyBytesWriter *writer)
3625
7.46M
{
3626
7.46M
    if (writer == NULL) {
3627
153k
        return;
3628
153k
    }
3629
3630
7.31M
    Py_XDECREF(writer->obj);
3631
7.31M
    _Py_FREELIST_FREE(bytes_writers, writer, PyMem_Free);
3632
7.31M
}
3633
3634
3635
PyObject*
3636
PyBytesWriter_FinishWithSize(PyBytesWriter *writer, Py_ssize_t size)
3637
6.94M
{
3638
6.94M
    PyObject *result;
3639
6.94M
    if (size == 0) {
3640
15.0k
        result = bytes_get_empty();
3641
15.0k
    }
3642
6.92M
    else if (writer->obj != NULL) {
3643
312k
        if (writer->use_bytearray) {
3644
0
            if (size != PyByteArray_GET_SIZE(writer->obj)) {
3645
0
                if (PyByteArray_Resize(writer->obj, size)) {
3646
0
                    goto error;
3647
0
                }
3648
0
            }
3649
0
        }
3650
312k
        else {
3651
312k
            if (size != PyBytes_GET_SIZE(writer->obj)) {
3652
309k
                if (_PyBytes_Resize(&writer->obj, size)) {
3653
0
                    goto error;
3654
0
                }
3655
309k
            }
3656
312k
        }
3657
312k
        result = writer->obj;
3658
312k
        writer->obj = NULL;
3659
312k
    }
3660
6.61M
    else if (writer->use_bytearray) {
3661
0
        result = PyByteArray_FromStringAndSize(writer->small_buffer, size);
3662
0
    }
3663
6.61M
    else {
3664
6.61M
        result = PyBytes_FromStringAndSize(writer->small_buffer, size);
3665
6.61M
    }
3666
6.94M
    PyBytesWriter_Discard(writer);
3667
6.94M
    return result;
3668
3669
0
error:
3670
0
    PyBytesWriter_Discard(writer);
3671
0
    return NULL;
3672
6.94M
}
3673
3674
PyObject*
3675
PyBytesWriter_Finish(PyBytesWriter *writer)
3676
531k
{
3677
531k
    return PyBytesWriter_FinishWithSize(writer, writer->size);
3678
531k
}
3679
3680
3681
PyObject*
3682
PyBytesWriter_FinishWithPointer(PyBytesWriter *writer, void *buf)
3683
6.40M
{
3684
6.40M
    Py_ssize_t size = (char*)buf - byteswriter_data(writer);
3685
6.40M
    if (size < 0 || size > byteswriter_allocated(writer)) {
3686
0
        PyBytesWriter_Discard(writer);
3687
0
        PyErr_SetString(PyExc_ValueError, "invalid end pointer");
3688
0
        return NULL;
3689
0
    }
3690
3691
6.40M
    return PyBytesWriter_FinishWithSize(writer, size);
3692
6.40M
}
3693
3694
3695
void*
3696
PyBytesWriter_GetData(PyBytesWriter *writer)
3697
7.45M
{
3698
7.45M
    return byteswriter_data(writer);
3699
7.45M
}
3700
3701
3702
Py_ssize_t
3703
PyBytesWriter_GetSize(PyBytesWriter *writer)
3704
0
{
3705
0
    return _PyBytesWriter_GetSize(writer);
3706
0
}
3707
3708
3709
static Py_ssize_t
3710
_PyBytesWriter_GetAllocated(PyBytesWriter *writer)
3711
138
{
3712
138
    return byteswriter_allocated(writer);
3713
138
}
3714
3715
3716
int
3717
PyBytesWriter_Resize(PyBytesWriter *writer, Py_ssize_t size)
3718
0
{
3719
0
    if (size < 0) {
3720
0
        PyErr_SetString(PyExc_ValueError, "size must be >= 0");
3721
0
        return -1;
3722
0
    }
3723
0
    if (byteswriter_resize(writer, size, 1) < 0) {
3724
0
        return -1;
3725
0
    }
3726
0
    writer->size = size;
3727
0
    return 0;
3728
0
}
3729
3730
3731
static void*
3732
_PyBytesWriter_ResizeAndUpdatePointer(PyBytesWriter *writer, Py_ssize_t size,
3733
                                      void *data)
3734
0
{
3735
0
    Py_ssize_t pos = (char*)data - byteswriter_data(writer);
3736
0
    if (PyBytesWriter_Resize(writer, size) < 0) {
3737
0
        return NULL;
3738
0
    }
3739
0
    return byteswriter_data(writer) + pos;
3740
0
}
3741
3742
3743
int
3744
PyBytesWriter_Grow(PyBytesWriter *writer, Py_ssize_t size)
3745
0
{
3746
0
    if (size < 0 && writer->size + size < 0) {
3747
0
        PyErr_SetString(PyExc_ValueError, "invalid size");
3748
0
        return -1;
3749
0
    }
3750
0
    if (size > PY_SSIZE_T_MAX - writer->size) {
3751
0
        PyErr_NoMemory();
3752
0
        return -1;
3753
0
    }
3754
0
    size = writer->size + size;
3755
3756
0
    if (byteswriter_resize(writer, size, 1) < 0) {
3757
0
        return -1;
3758
0
    }
3759
0
    writer->size = size;
3760
0
    return 0;
3761
0
}
3762
3763
3764
void*
3765
PyBytesWriter_GrowAndUpdatePointer(PyBytesWriter *writer, Py_ssize_t size,
3766
                                   void *buf)
3767
0
{
3768
0
    Py_ssize_t pos = (char*)buf - byteswriter_data(writer);
3769
0
    if (PyBytesWriter_Grow(writer, size) < 0) {
3770
0
        return NULL;
3771
0
    }
3772
0
    return byteswriter_data(writer) + pos;
3773
0
}
3774
3775
3776
int
3777
PyBytesWriter_WriteBytes(PyBytesWriter *writer,
3778
                         const void *bytes, Py_ssize_t size)
3779
0
{
3780
0
    if (size < 0) {
3781
0
        size_t len = strlen(bytes);
3782
0
        if (len > (size_t)PY_SSIZE_T_MAX) {
3783
0
            PyErr_NoMemory();
3784
0
            return -1;
3785
0
        }
3786
0
        size = (Py_ssize_t)len;
3787
0
    }
3788
3789
0
    Py_ssize_t pos = writer->size;
3790
0
    if (PyBytesWriter_Grow(writer, size) < 0) {
3791
0
        return -1;
3792
0
    }
3793
0
    char *buf = byteswriter_data(writer);
3794
0
    memcpy(buf + pos, bytes, size);
3795
0
    return 0;
3796
0
}
3797
3798
3799
int
3800
PyBytesWriter_Format(PyBytesWriter *writer, const char *format, ...)
3801
0
{
3802
0
    Py_ssize_t pos = writer->size;
3803
0
    if (PyBytesWriter_Grow(writer, strlen(format)) < 0) {
3804
0
        return -1;
3805
0
    }
3806
3807
0
    va_list vargs;
3808
0
    va_start(vargs, format);
3809
0
    char *buf = bytes_fromformat(writer, pos, format, vargs);
3810
0
    va_end(vargs);
3811
3812
0
    Py_ssize_t size = buf - byteswriter_data(writer);
3813
0
    return PyBytesWriter_Resize(writer, size);
3814
0
}