Coverage Report

Created: 2025-10-10 06:33

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