Coverage Report

Created: 2026-12-01 06:09

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