Coverage Report

Created: 2025-07-18 06:09

/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
43.8k
{
10
43.8k
    const char *sepstr = STRINGLIB_STR(sep);
11
43.8k
    Py_ssize_t seplen = STRINGLIB_LEN(sep);
12
43.8k
    PyObject *res = NULL;
13
43.8k
    char *p;
14
43.8k
    Py_ssize_t seqlen = 0;
15
43.8k
    Py_ssize_t sz = 0;
16
43.8k
    Py_ssize_t i, nbufs;
17
43.8k
    PyObject *seq, *item;
18
43.8k
    Py_buffer *buffers = NULL;
19
43.8k
#define NB_STATIC_BUFFERS 10
20
43.8k
    Py_buffer static_buffers[NB_STATIC_BUFFERS];
21
43.8k
#define GIL_THRESHOLD 1048576
22
43.8k
    int drop_gil = 1;
23
43.8k
    PyThreadState *save = NULL;
24
25
43.8k
    seq = PySequence_Fast(iterable, "can only join an iterable");
26
43.8k
    if (seq == NULL) {
27
0
        return NULL;
28
0
    }
29
30
43.8k
    seqlen = PySequence_Fast_GET_SIZE(seq);
31
43.8k
    if (seqlen == 0) {
32
0
        Py_DECREF(seq);
33
0
        return STRINGLIB_NEW(NULL, 0);
34
0
    }
35
#if !STRINGLIB_MUTABLE
36
43.8k
    if (seqlen == 1) {
37
9.51k
        item = PySequence_Fast_GET_ITEM(seq, 0);
38
9.51k
        if (STRINGLIB_CHECK_EXACT(item)) {
39
9.51k
            Py_INCREF(item);
40
9.51k
            Py_DECREF(seq);
41
9.51k
            return item;
42
9.51k
        }
43
9.51k
    }
44
34.3k
#endif
45
34.3k
    if (seqlen > NB_STATIC_BUFFERS) {
46
1.99k
        buffers = PyMem_NEW(Py_buffer, seqlen);
47
1.99k
        if (buffers == NULL) {
48
0
            Py_DECREF(seq);
49
0
            PyErr_NoMemory();
50
0
            return NULL;
51
0
        }
52
1.99k
    }
53
32.3k
    else {
54
32.3k
        buffers = static_buffers;
55
32.3k
    }
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
199k
    for (i = 0, nbufs = 0; i < seqlen; i++) {
62
164k
        Py_ssize_t itemlen;
63
164k
        item = PySequence_Fast_GET_ITEM(seq, i);
64
164k
        if (PyBytes_CheckExact(item)) {
65
            /* Fast path. */
66
164k
            buffers[i].obj = Py_NewRef(item);
67
164k
            buffers[i].buf = PyBytes_AS_STRING(item);
68
164k
            buffers[i].len = PyBytes_GET_SIZE(item);
69
164k
        }
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
164k
        nbufs = i + 1;  /* for error cleanup */
87
164k
        itemlen = buffers[i].len;
88
164k
        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
164k
        sz += itemlen;
94
164k
        if (i != 0) {
95
130k
            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
130k
            sz += seplen;
101
130k
        }
102
164k
        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
164k
    }
108
109
    /* Allocate result space. */
110
34.3k
    res = STRINGLIB_NEW(NULL, sz);
111
34.3k
    if (res == NULL)
112
0
        goto error;
113
114
    /* Catenate everything. */
115
34.3k
    p = STRINGLIB_STR(res);
116
34.3k
    if (sz < GIL_THRESHOLD) {
117
34.3k
        drop_gil = 0;   /* Benefits are likely outweighed by the overheads */
118
34.3k
    }
119
34.3k
    if (drop_gil) {
120
0
        save = PyEval_SaveThread();
121
0
    }
122
34.3k
    if (!seplen) {
123
        /* fast path */
124
199k
        for (i = 0; i < nbufs; i++) {
125
164k
            Py_ssize_t n = buffers[i].len;
126
164k
            char *q = buffers[i].buf;
127
164k
            memcpy(p, q, n);
128
164k
            p += n;
129
164k
        }
130
34.3k
    }
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
34.3k
    if (drop_gil) {
146
0
        PyEval_RestoreThread(save);
147
0
    }
148
34.3k
    goto done;
149
150
0
error:
151
0
    res = NULL;
152
34.3k
done:
153
34.3k
    Py_DECREF(seq);
154
199k
    for (i = 0; i < nbufs; i++)
155
164k
        PyBuffer_Release(&buffers[i]);
156
34.3k
    if (buffers != static_buffers)
157
1.99k
        PyMem_Free(buffers);
158
34.3k
    return res;
159
0
}
bytesobject.c:stringlib_bytes_join
Line
Count
Source
9
43.8k
{
10
43.8k
    const char *sepstr = STRINGLIB_STR(sep);
11
43.8k
    Py_ssize_t seplen = STRINGLIB_LEN(sep);
12
43.8k
    PyObject *res = NULL;
13
43.8k
    char *p;
14
43.8k
    Py_ssize_t seqlen = 0;
15
43.8k
    Py_ssize_t sz = 0;
16
43.8k
    Py_ssize_t i, nbufs;
17
43.8k
    PyObject *seq, *item;
18
43.8k
    Py_buffer *buffers = NULL;
19
43.8k
#define NB_STATIC_BUFFERS 10
20
43.8k
    Py_buffer static_buffers[NB_STATIC_BUFFERS];
21
43.8k
#define GIL_THRESHOLD 1048576
22
43.8k
    int drop_gil = 1;
23
43.8k
    PyThreadState *save = NULL;
24
25
43.8k
    seq = PySequence_Fast(iterable, "can only join an iterable");
26
43.8k
    if (seq == NULL) {
27
0
        return NULL;
28
0
    }
29
30
43.8k
    seqlen = PySequence_Fast_GET_SIZE(seq);
31
43.8k
    if (seqlen == 0) {
32
0
        Py_DECREF(seq);
33
0
        return STRINGLIB_NEW(NULL, 0);
34
0
    }
35
43.8k
#if !STRINGLIB_MUTABLE
36
43.8k
    if (seqlen == 1) {
37
9.51k
        item = PySequence_Fast_GET_ITEM(seq, 0);
38
9.51k
        if (STRINGLIB_CHECK_EXACT(item)) {
39
9.51k
            Py_INCREF(item);
40
9.51k
            Py_DECREF(seq);
41
9.51k
            return item;
42
9.51k
        }
43
9.51k
    }
44
34.3k
#endif
45
34.3k
    if (seqlen > NB_STATIC_BUFFERS) {
46
1.99k
        buffers = PyMem_NEW(Py_buffer, seqlen);
47
1.99k
        if (buffers == NULL) {
48
0
            Py_DECREF(seq);
49
0
            PyErr_NoMemory();
50
0
            return NULL;
51
0
        }
52
1.99k
    }
53
32.3k
    else {
54
32.3k
        buffers = static_buffers;
55
32.3k
    }
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
199k
    for (i = 0, nbufs = 0; i < seqlen; i++) {
62
164k
        Py_ssize_t itemlen;
63
164k
        item = PySequence_Fast_GET_ITEM(seq, i);
64
164k
        if (PyBytes_CheckExact(item)) {
65
            /* Fast path. */
66
164k
            buffers[i].obj = Py_NewRef(item);
67
164k
            buffers[i].buf = PyBytes_AS_STRING(item);
68
164k
            buffers[i].len = PyBytes_GET_SIZE(item);
69
164k
        }
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
164k
        nbufs = i + 1;  /* for error cleanup */
87
164k
        itemlen = buffers[i].len;
88
164k
        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
164k
        sz += itemlen;
94
164k
        if (i != 0) {
95
130k
            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
130k
            sz += seplen;
101
130k
        }
102
164k
        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
164k
    }
108
109
    /* Allocate result space. */
110
34.3k
    res = STRINGLIB_NEW(NULL, sz);
111
34.3k
    if (res == NULL)
112
0
        goto error;
113
114
    /* Catenate everything. */
115
34.3k
    p = STRINGLIB_STR(res);
116
34.3k
    if (sz < GIL_THRESHOLD) {
117
34.3k
        drop_gil = 0;   /* Benefits are likely outweighed by the overheads */
118
34.3k
    }
119
34.3k
    if (drop_gil) {
120
0
        save = PyEval_SaveThread();
121
0
    }
122
34.3k
    if (!seplen) {
123
        /* fast path */
124
199k
        for (i = 0; i < nbufs; i++) {
125
164k
            Py_ssize_t n = buffers[i].len;
126
164k
            char *q = buffers[i].buf;
127
164k
            memcpy(p, q, n);
128
164k
            p += n;
129
164k
        }
130
34.3k
    }
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
34.3k
    if (drop_gil) {
146
0
        PyEval_RestoreThread(save);
147
0
    }
148
34.3k
    goto done;
149
150
0
error:
151
0
    res = NULL;
152
34.3k
done:
153
34.3k
    Py_DECREF(seq);
154
199k
    for (i = 0; i < nbufs; i++)
155
164k
        PyBuffer_Release(&buffers[i]);
156
34.3k
    if (buffers != static_buffers)
157
1.99k
        PyMem_Free(buffers);
158
34.3k
    return res;
159
0
}
Unexecuted instantiation: bytearrayobject.c:stringlib_bytes_join
160
161
#undef NB_STATIC_BUFFERS
162
#undef GIL_THRESHOLD