Coverage Report

Created: 2025-07-11 06:24

/src/cpython/Objects/stringlib/join.h
Line
Count
Source (jump to first uncovered line)
1
/* stringlib: bytes joining implementation */
2
3
#if STRINGLIB_IS_UNICODE
4
#error join.h only compatible with byte-wise strings
5
#endif
6
7
Py_LOCAL_INLINE(PyObject *)
8
STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
9
39.0k
{
10
39.0k
    const char *sepstr = STRINGLIB_STR(sep);
11
39.0k
    Py_ssize_t seplen = STRINGLIB_LEN(sep);
12
39.0k
    PyObject *res = NULL;
13
39.0k
    char *p;
14
39.0k
    Py_ssize_t seqlen = 0;
15
39.0k
    Py_ssize_t sz = 0;
16
39.0k
    Py_ssize_t i, nbufs;
17
39.0k
    PyObject *seq, *item;
18
39.0k
    Py_buffer *buffers = NULL;
19
39.0k
#define NB_STATIC_BUFFERS 10
20
39.0k
    Py_buffer static_buffers[NB_STATIC_BUFFERS];
21
39.0k
#define GIL_THRESHOLD 1048576
22
39.0k
    int drop_gil = 1;
23
39.0k
    PyThreadState *save = NULL;
24
25
39.0k
    seq = PySequence_Fast(iterable, "can only join an iterable");
26
39.0k
    if (seq == NULL) {
27
0
        return NULL;
28
0
    }
29
30
39.0k
    seqlen = PySequence_Fast_GET_SIZE(seq);
31
39.0k
    if (seqlen == 0) {
32
0
        Py_DECREF(seq);
33
0
        return STRINGLIB_NEW(NULL, 0);
34
0
    }
35
#if !STRINGLIB_MUTABLE
36
39.0k
    if (seqlen == 1) {
37
7.35k
        item = PySequence_Fast_GET_ITEM(seq, 0);
38
7.35k
        if (STRINGLIB_CHECK_EXACT(item)) {
39
7.35k
            Py_INCREF(item);
40
7.35k
            Py_DECREF(seq);
41
7.35k
            return item;
42
7.35k
        }
43
7.35k
    }
44
31.6k
#endif
45
31.6k
    if (seqlen > NB_STATIC_BUFFERS) {
46
1.64k
        buffers = PyMem_NEW(Py_buffer, seqlen);
47
1.64k
        if (buffers == NULL) {
48
0
            Py_DECREF(seq);
49
0
            PyErr_NoMemory();
50
0
            return NULL;
51
0
        }
52
1.64k
    }
53
30.0k
    else {
54
30.0k
        buffers = static_buffers;
55
30.0k
    }
56
57
    /* Here is the general case.  Do a pre-pass to figure out the total
58
     * amount of space we'll need (sz), and see whether all arguments are
59
     * bytes-like.
60
     */
61
174k
    for (i = 0, nbufs = 0; i < seqlen; i++) {
62
143k
        Py_ssize_t itemlen;
63
143k
        item = PySequence_Fast_GET_ITEM(seq, i);
64
143k
        if (PyBytes_CheckExact(item)) {
65
            /* Fast path. */
66
143k
            buffers[i].obj = Py_NewRef(item);
67
143k
            buffers[i].buf = PyBytes_AS_STRING(item);
68
143k
            buffers[i].len = PyBytes_GET_SIZE(item);
69
143k
        }
70
0
        else {
71
0
            if (PyObject_GetBuffer(item, &buffers[i], PyBUF_SIMPLE) != 0) {
72
0
                PyErr_Format(PyExc_TypeError,
73
0
                             "sequence item %zd: expected a bytes-like object, "
74
0
                             "%.80s found",
75
0
                             i, Py_TYPE(item)->tp_name);
76
0
                goto error;
77
0
            }
78
            /* If the backing objects are mutable, then dropping the GIL
79
             * opens up race conditions where another thread tries to modify
80
             * the object which we hold a buffer on it. Such code has data
81
             * races anyway, but this is a conservative approach that avoids
82
             * changing the behaviour of that data race.
83
             */
84
0
            drop_gil = 0;
85
0
        }
86
143k
        nbufs = i + 1;  /* for error cleanup */
87
143k
        itemlen = buffers[i].len;
88
143k
        if (itemlen > PY_SSIZE_T_MAX - sz) {
89
0
            PyErr_SetString(PyExc_OverflowError,
90
0
                            "join() result is too long");
91
0
            goto error;
92
0
        }
93
143k
        sz += itemlen;
94
143k
        if (i != 0) {
95
111k
            if (seplen > PY_SSIZE_T_MAX - sz) {
96
0
                PyErr_SetString(PyExc_OverflowError,
97
0
                                "join() result is too long");
98
0
                goto error;
99
0
            }
100
111k
            sz += seplen;
101
111k
        }
102
143k
        if (seqlen != PySequence_Fast_GET_SIZE(seq)) {
103
0
            PyErr_SetString(PyExc_RuntimeError,
104
0
                            "sequence changed size during iteration");
105
0
            goto error;
106
0
        }
107
143k
    }
108
109
    /* Allocate result space. */
110
31.6k
    res = STRINGLIB_NEW(NULL, sz);
111
31.6k
    if (res == NULL)
112
0
        goto error;
113
114
    /* Catenate everything. */
115
31.6k
    p = STRINGLIB_STR(res);
116
31.6k
    if (sz < GIL_THRESHOLD) {
117
31.6k
        drop_gil = 0;   /* Benefits are likely outweighed by the overheads */
118
31.6k
    }
119
31.6k
    if (drop_gil) {
120
0
        save = PyEval_SaveThread();
121
0
    }
122
31.6k
    if (!seplen) {
123
        /* fast path */
124
174k
        for (i = 0; i < nbufs; i++) {
125
143k
            Py_ssize_t n = buffers[i].len;
126
143k
            char *q = buffers[i].buf;
127
143k
            memcpy(p, q, n);
128
143k
            p += n;
129
143k
        }
130
31.6k
    }
131
0
    else {
132
0
        for (i = 0; i < nbufs; i++) {
133
0
            Py_ssize_t n;
134
0
            char *q;
135
0
            if (i) {
136
0
                memcpy(p, sepstr, seplen);
137
0
                p += seplen;
138
0
            }
139
0
            n = buffers[i].len;
140
0
            q = buffers[i].buf;
141
0
            memcpy(p, q, n);
142
0
            p += n;
143
0
        }
144
0
    }
145
31.6k
    if (drop_gil) {
146
0
        PyEval_RestoreThread(save);
147
0
    }
148
31.6k
    goto done;
149
150
0
error:
151
0
    res = NULL;
152
31.6k
done:
153
31.6k
    Py_DECREF(seq);
154
174k
    for (i = 0; i < nbufs; i++)
155
143k
        PyBuffer_Release(&buffers[i]);
156
31.6k
    if (buffers != static_buffers)
157
1.64k
        PyMem_Free(buffers);
158
31.6k
    return res;
159
0
}
bytesobject.c:stringlib_bytes_join
Line
Count
Source
9
39.0k
{
10
39.0k
    const char *sepstr = STRINGLIB_STR(sep);
11
39.0k
    Py_ssize_t seplen = STRINGLIB_LEN(sep);
12
39.0k
    PyObject *res = NULL;
13
39.0k
    char *p;
14
39.0k
    Py_ssize_t seqlen = 0;
15
39.0k
    Py_ssize_t sz = 0;
16
39.0k
    Py_ssize_t i, nbufs;
17
39.0k
    PyObject *seq, *item;
18
39.0k
    Py_buffer *buffers = NULL;
19
39.0k
#define NB_STATIC_BUFFERS 10
20
39.0k
    Py_buffer static_buffers[NB_STATIC_BUFFERS];
21
39.0k
#define GIL_THRESHOLD 1048576
22
39.0k
    int drop_gil = 1;
23
39.0k
    PyThreadState *save = NULL;
24
25
39.0k
    seq = PySequence_Fast(iterable, "can only join an iterable");
26
39.0k
    if (seq == NULL) {
27
0
        return NULL;
28
0
    }
29
30
39.0k
    seqlen = PySequence_Fast_GET_SIZE(seq);
31
39.0k
    if (seqlen == 0) {
32
0
        Py_DECREF(seq);
33
0
        return STRINGLIB_NEW(NULL, 0);
34
0
    }
35
39.0k
#if !STRINGLIB_MUTABLE
36
39.0k
    if (seqlen == 1) {
37
7.35k
        item = PySequence_Fast_GET_ITEM(seq, 0);
38
7.35k
        if (STRINGLIB_CHECK_EXACT(item)) {
39
7.35k
            Py_INCREF(item);
40
7.35k
            Py_DECREF(seq);
41
7.35k
            return item;
42
7.35k
        }
43
7.35k
    }
44
31.6k
#endif
45
31.6k
    if (seqlen > NB_STATIC_BUFFERS) {
46
1.64k
        buffers = PyMem_NEW(Py_buffer, seqlen);
47
1.64k
        if (buffers == NULL) {
48
0
            Py_DECREF(seq);
49
0
            PyErr_NoMemory();
50
0
            return NULL;
51
0
        }
52
1.64k
    }
53
30.0k
    else {
54
30.0k
        buffers = static_buffers;
55
30.0k
    }
56
57
    /* Here is the general case.  Do a pre-pass to figure out the total
58
     * amount of space we'll need (sz), and see whether all arguments are
59
     * bytes-like.
60
     */
61
174k
    for (i = 0, nbufs = 0; i < seqlen; i++) {
62
143k
        Py_ssize_t itemlen;
63
143k
        item = PySequence_Fast_GET_ITEM(seq, i);
64
143k
        if (PyBytes_CheckExact(item)) {
65
            /* Fast path. */
66
143k
            buffers[i].obj = Py_NewRef(item);
67
143k
            buffers[i].buf = PyBytes_AS_STRING(item);
68
143k
            buffers[i].len = PyBytes_GET_SIZE(item);
69
143k
        }
70
0
        else {
71
0
            if (PyObject_GetBuffer(item, &buffers[i], PyBUF_SIMPLE) != 0) {
72
0
                PyErr_Format(PyExc_TypeError,
73
0
                             "sequence item %zd: expected a bytes-like object, "
74
0
                             "%.80s found",
75
0
                             i, Py_TYPE(item)->tp_name);
76
0
                goto error;
77
0
            }
78
            /* If the backing objects are mutable, then dropping the GIL
79
             * opens up race conditions where another thread tries to modify
80
             * the object which we hold a buffer on it. Such code has data
81
             * races anyway, but this is a conservative approach that avoids
82
             * changing the behaviour of that data race.
83
             */
84
0
            drop_gil = 0;
85
0
        }
86
143k
        nbufs = i + 1;  /* for error cleanup */
87
143k
        itemlen = buffers[i].len;
88
143k
        if (itemlen > PY_SSIZE_T_MAX - sz) {
89
0
            PyErr_SetString(PyExc_OverflowError,
90
0
                            "join() result is too long");
91
0
            goto error;
92
0
        }
93
143k
        sz += itemlen;
94
143k
        if (i != 0) {
95
111k
            if (seplen > PY_SSIZE_T_MAX - sz) {
96
0
                PyErr_SetString(PyExc_OverflowError,
97
0
                                "join() result is too long");
98
0
                goto error;
99
0
            }
100
111k
            sz += seplen;
101
111k
        }
102
143k
        if (seqlen != PySequence_Fast_GET_SIZE(seq)) {
103
0
            PyErr_SetString(PyExc_RuntimeError,
104
0
                            "sequence changed size during iteration");
105
0
            goto error;
106
0
        }
107
143k
    }
108
109
    /* Allocate result space. */
110
31.6k
    res = STRINGLIB_NEW(NULL, sz);
111
31.6k
    if (res == NULL)
112
0
        goto error;
113
114
    /* Catenate everything. */
115
31.6k
    p = STRINGLIB_STR(res);
116
31.6k
    if (sz < GIL_THRESHOLD) {
117
31.6k
        drop_gil = 0;   /* Benefits are likely outweighed by the overheads */
118
31.6k
    }
119
31.6k
    if (drop_gil) {
120
0
        save = PyEval_SaveThread();
121
0
    }
122
31.6k
    if (!seplen) {
123
        /* fast path */
124
174k
        for (i = 0; i < nbufs; i++) {
125
143k
            Py_ssize_t n = buffers[i].len;
126
143k
            char *q = buffers[i].buf;
127
143k
            memcpy(p, q, n);
128
143k
            p += n;
129
143k
        }
130
31.6k
    }
131
0
    else {
132
0
        for (i = 0; i < nbufs; i++) {
133
0
            Py_ssize_t n;
134
0
            char *q;
135
0
            if (i) {
136
0
                memcpy(p, sepstr, seplen);
137
0
                p += seplen;
138
0
            }
139
0
            n = buffers[i].len;
140
0
            q = buffers[i].buf;
141
0
            memcpy(p, q, n);
142
0
            p += n;
143
0
        }
144
0
    }
145
31.6k
    if (drop_gil) {
146
0
        PyEval_RestoreThread(save);
147
0
    }
148
31.6k
    goto done;
149
150
0
error:
151
0
    res = NULL;
152
31.6k
done:
153
31.6k
    Py_DECREF(seq);
154
174k
    for (i = 0; i < nbufs; i++)
155
143k
        PyBuffer_Release(&buffers[i]);
156
31.6k
    if (buffers != static_buffers)
157
1.64k
        PyMem_Free(buffers);
158
31.6k
    return res;
159
0
}
Unexecuted instantiation: bytearrayobject.c:stringlib_bytes_join
160
161
#undef NB_STATIC_BUFFERS
162
#undef GIL_THRESHOLD