Coverage Report

Created: 2022-07-08 09:39

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