Coverage Report

Created: 2026-11-20 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/Parser/tokenizer/decoder.c
Line
Count
Source
1
#include "Python.h"
2
#include "pycore_codecs.h"
3
#include "pycore_global_strings.h"
4
#include "pycore_runtime.h"
5
#include "errcode.h"
6
7
#include "reader_internal.h"
8
#include "helpers.h"
9
#include "../lexer/state.h"
10
11
0
#define UNICODE_BOM 0xFEFF
12
0
#define UTF16_LE_BOM "\xff\xfe"
13
0
#define UTF16_BE_BOM "\xfe\xff"
14
0
#define UTF16_BOM_LENGTH ((Py_ssize_t)(sizeof(UTF16_LE_BOM) - 1))
15
42.2k
#define UTF8_BOM "\xef\xbb\xbf"
16
43.1k
#define UTF8_BOM_LENGTH ((Py_ssize_t)(sizeof(UTF8_BOM) - 1))
17
18
char *
19
_PyTok_CopyBytes(const char *data, Py_ssize_t len)
20
16.4k
{
21
16.4k
    if (len < 0 || len == PY_SSIZE_T_MAX) {
22
0
        PyErr_NoMemory();
23
0
        return NULL;
24
0
    }
25
16.4k
    char *copy = PyMem_Malloc((size_t)len + 1);
26
16.4k
    if (copy == NULL) {
27
0
        PyErr_NoMemory();
28
0
        return NULL;
29
0
    }
30
16.4k
    memcpy(copy, data, (size_t)len);
31
16.4k
    copy[len] = '\0';
32
16.4k
    return copy;
33
16.4k
}
34
35
static void
36
chunk_release_data(_PyTok_Chunk *chunk)
37
478k
{
38
478k
    switch (chunk->ownership) {
39
475k
        case _PYTOK_CHUNK_BORROWED:
40
475k
            break;
41
0
        case _PYTOK_CHUNK_PYMEM:
42
0
            PyMem_Free(chunk->data);
43
0
            break;
44
3.18k
        case _PYTOK_CHUNK_PYOBJECT:
45
3.18k
            Py_DECREF(chunk->owner);
46
3.18k
            break;
47
478k
    }
48
478k
}
49
50
void
51
_PyTok_ChunkClear(_PyTok_Chunk *chunk)
52
475k
{
53
475k
    chunk_release_data(chunk);
54
475k
    *chunk = (_PyTok_Chunk){0};
55
475k
}
56
57
static int
58
chunk_set_unicode(struct tok_state *tok, _PyTok_Chunk *chunk,
59
                  PyObject *unicode, int strip_bom)
60
3.21k
{
61
3.21k
    Py_ssize_t utf8_len;
62
3.21k
    const char *utf8 = PyUnicode_AsUTF8AndSize(unicode, &utf8_len);
63
3.21k
    if (utf8 == NULL) {
64
34
        Py_DECREF(unicode);
65
34
        tok->done = PyErr_ExceptionMatches(PyExc_MemoryError)
66
34
            ? E_NOMEM : E_DECODE;
67
34
        return -1;
68
34
    }
69
3.18k
    if (strip_bom && PyUnicode_GET_LENGTH(unicode) > 0 &&
70
0
            PyUnicode_ReadChar(unicode, 0) == UNICODE_BOM) {
71
0
        utf8 += UTF8_BOM_LENGTH;
72
0
        utf8_len -= UTF8_BOM_LENGTH;
73
0
    }
74
3.18k
    chunk_release_data(chunk);
75
3.18k
    chunk->owner = unicode;
76
3.18k
    chunk->data = (char *)utf8;
77
3.18k
    chunk->len = utf8_len;
78
3.18k
    chunk->ownership = _PYTOK_CHUNK_PYOBJECT;
79
3.18k
    return 0;
80
3.21k
}
81
82
char *
83
_PyTok_NormalizeNewlines(const char *data, Py_ssize_t len,
84
                         Py_ssize_t *out_len)
85
0
{
86
0
    if (len < 0 || len == PY_SSIZE_T_MAX) {
87
0
        PyErr_NoMemory();
88
0
        return NULL;
89
0
    }
90
0
    char *result = PyMem_Malloc((size_t)len + 1);
91
0
    if (result == NULL) {
92
0
        PyErr_NoMemory();
93
0
        return NULL;
94
0
    }
95
0
    Py_ssize_t write = 0;
96
0
    for (Py_ssize_t read = 0; read < len; read++) {
97
0
        char c = data[read];
98
0
        if (c == '\r') {
99
0
            if (read + 1 < len && data[read + 1] == '\n') {
100
0
                read++;
101
0
            }
102
0
            c = '\n';
103
0
        }
104
0
        result[write++] = c;
105
0
    }
106
0
    result[write] = '\0';
107
0
    *out_len = write;
108
0
    return result;
109
0
}
110
111
int
112
_PyTok_SetEncoding(struct tok_state *tok, const char *encoding)
113
11.7k
{
114
11.7k
    char *copy = _PyTok_CopyBytes(encoding, strlen(encoding));
115
11.7k
    if (copy == NULL) {
116
0
        tok->done = E_NOMEM;
117
0
        return -1;
118
0
    }
119
11.7k
    PyMem_Free(tok->reader->encoding);
120
11.7k
    tok->reader->encoding = copy;
121
11.7k
    return 0;
122
11.7k
}
123
124
static int
125
find_cookie(const char *line, Py_ssize_t len, char **encoding, int *scan_next)
126
15.5k
{
127
15.5k
    Py_ssize_t i = 0;
128
15.5k
    *encoding = NULL;
129
15.5k
    *scan_next = 1;
130
16.9k
    for (; i < len; i++) {
131
16.8k
        if (line[i] == '#') {
132
5.02k
            break;
133
5.02k
        }
134
11.8k
        if (line[i] == '\n' || line[i] == '\r') {
135
780
            return 0;
136
780
        }
137
11.0k
        if (line[i] != ' ' && line[i] != '\t' && line[i] != '\f') {
138
9.66k
            *scan_next = 0;
139
9.66k
            return 0;
140
9.66k
        }
141
11.0k
    }
142
5.06k
    const Py_ssize_t marker_len = sizeof("coding") - 1;
143
228k
    for (; i < len - marker_len; i++) {
144
227k
        if (memcmp(line + i, "coding", marker_len) != 0) {
145
222k
            continue;
146
222k
        }
147
5.34k
        const char *cursor = line + i + marker_len;
148
5.34k
        if (*cursor != ':' && *cursor != '=') {
149
292
            continue;
150
292
        }
151
8.99k
        do {
152
8.99k
            cursor++;
153
8.99k
        } while (cursor < line + len &&
154
8.98k
                 (*cursor == ' ' || *cursor == '\t'));
155
5.04k
        const char *start = cursor;
156
66.9k
        while (cursor < line + len &&
157
66.6k
                (Py_ISALNUM(*cursor) || *cursor == '-' ||
158
61.9k
                 *cursor == '_' || *cursor == '.')) {
159
61.9k
            cursor++;
160
61.9k
        }
161
5.04k
        if (cursor == start) {
162
363
            continue;
163
363
        }
164
4.68k
        Py_ssize_t encoding_len = cursor - start;
165
        // Include the longest recognized prefix and its NUL terminator.
166
4.68k
        char normalized[sizeof("iso-latin-1-")];
167
4.68k
        const Py_ssize_t prefix_len = Py_MIN(
168
4.68k
            encoding_len, (Py_ssize_t)sizeof(normalized) - 1);
169
4.68k
        Py_ssize_t n;
170
29.2k
        for (n = 0; n < prefix_len; n++) {
171
24.6k
            normalized[n] = start[n] == '_' ? '-' : Py_TOLOWER(start[n]);
172
24.6k
        }
173
4.68k
        normalized[n] = '\0';
174
4.68k
        const char *canonical = start;
175
4.68k
        if (strcmp(normalized, "utf-8") == 0 ||
176
4.68k
                strncmp(normalized, "utf-8-", sizeof("utf-8-") - 1) == 0) {
177
2
            canonical = "utf-8";
178
2
        }
179
4.68k
        else if (strcmp(normalized, "latin-1") == 0 ||
180
4.66k
                 strcmp(normalized, "iso-8859-1") == 0 ||
181
4.66k
                 strcmp(normalized, "iso-latin-1") == 0 ||
182
4.66k
                 strncmp(normalized, "latin-1-", sizeof("latin-1-") - 1) == 0 ||
183
4.66k
                 strncmp(normalized, "iso-8859-1-", sizeof("iso-8859-1-") - 1) == 0 ||
184
4.66k
                 strncmp(normalized, "iso-latin-1-", sizeof("iso-latin-1-") - 1) == 0) {
185
23
            canonical = "iso-8859-1";
186
23
        }
187
4.68k
        if (canonical != start) {
188
25
            encoding_len = strlen(canonical);
189
25
        }
190
4.68k
        *encoding = _PyTok_CopyBytes(canonical, encoding_len);
191
4.68k
        if (*encoding == NULL) {
192
0
            return -1;
193
0
        }
194
4.68k
        *scan_next = 0;
195
4.68k
        return 0;
196
4.68k
    }
197
379
    return 0;
198
5.06k
}
199
200
_PyTok_EncodingResult
201
_PyTok_DetectEncoding(struct tok_state *tok, const _PyTok_Chunk *first,
202
                      const _PyTok_Chunk *second, int final,
203
                      Py_ssize_t *bom_len)
204
14.6k
{
205
14.6k
    int bom = first->len >= UTF8_BOM_LENGTH &&
206
13.7k
        memcmp(first->data, UTF8_BOM, UTF8_BOM_LENGTH) == 0;
207
14.6k
    *bom_len = bom ? UTF8_BOM_LENGTH : 0;
208
209
14.6k
    char *cookie = NULL;
210
14.6k
    int scan_next = 0;
211
14.6k
    int cookie_line = 1;
212
14.6k
    const char *first_data = first->data + *bom_len;
213
14.6k
    Py_ssize_t first_len = first->len - *bom_len;
214
14.6k
    if (find_cookie(first_data, first_len, &cookie, &scan_next) < 0) {
215
0
        return _PYTOK_ENCODING_ERROR;
216
0
    }
217
14.6k
    if (cookie == NULL && scan_next && second != NULL) {
218
811
        if (find_cookie(second->data, second->len, &cookie, &scan_next) < 0) {
219
0
            return _PYTOK_ENCODING_ERROR;
220
0
        }
221
811
        cookie_line = 2;
222
811
    }
223
13.8k
    else if (cookie == NULL && scan_next && !final) {
224
0
        return _PYTOK_ENCODING_NEED_SECOND_LINE;
225
0
    }
226
227
14.6k
    if (cookie == NULL) {
228
10.0k
        if (bom && _PyTok_SetEncoding(tok, "utf-8") < 0) {
229
0
            return _PYTOK_ENCODING_ERROR;
230
0
        }
231
10.0k
        return _PYTOK_ENCODING_DONE;
232
10.0k
    }
233
4.68k
    if (bom && strcmp(cookie, "utf-8") != 0) {
234
7
        const _PyTok_Chunk *line = cookie_line == 2 ? second : first;
235
7
        const char *line_data = line->data + (cookie_line == 1 ? *bom_len : 0);
236
7
        Py_ssize_t line_len = line->len - (cookie_line == 1 ? *bom_len : 0);
237
7
        int end_col = (int)Py_MIN(line_len, INT_MAX);
238
7
        if (end_col > 0 && (line_data[end_col - 1] == '\n' ||
239
5
                            line_data[end_col - 1] == '\r')) {
240
4
            end_col--;
241
4
        }
242
7
        _PyTokenizer_syntaxerror_at(
243
7
            tok, line_data, 0, cookie_line, 0, end_col,
244
7
            "encoding problem: %s with BOM", cookie);
245
7
        PyMem_Free(cookie);
246
7
        return _PYTOK_ENCODING_ERROR;
247
7
    }
248
4.67k
    PyMem_Free(tok->reader->encoding);
249
4.67k
    tok->reader->encoding = cookie;
250
4.67k
    return _PYTOK_ENCODING_DONE;
251
4.68k
}
252
253
int
254
_PyTok_DecodeOnce(struct tok_state *tok, _PyTok_Chunk *chunk,
255
                  const char *encoding, const char *errors)
256
4.67k
{
257
4.67k
    PyObject *unicode = PyUnicode_Decode(
258
4.67k
        chunk->data, chunk->len, encoding, errors);
259
4.67k
    if (unicode == NULL) {
260
1.46k
        tok->done = PyErr_ExceptionMatches(PyExc_MemoryError)
261
1.46k
            ? E_NOMEM : E_DECODE;
262
1.46k
        return -1;
263
1.46k
    }
264
3.21k
    return chunk_set_unicode(tok, chunk, unicode, 0);
265
4.67k
}
266
267
static Py_ssize_t
268
raw_line_length(const char *data, Py_ssize_t len)
269
424k
{
270
55.6M
    for (Py_ssize_t i = 0; i < len; i++) {
271
55.6M
        if (data[i] == '\n') {
272
262k
            return i + 1;
273
262k
        }
274
55.3M
        if (data[i] == '\r') {
275
127k
            return i + 1 < len && data[i + 1] == '\n' ? i + 2 : i + 1;
276
127k
        }
277
55.3M
    }
278
35.1k
    return len;
279
424k
}
280
281
static int
282
store_prepared_source(struct tok_state *tok, const char *data, Py_ssize_t len,
283
                      int preserve_crlf, int add_final_newline)
284
24.9k
{
285
24.9k
    Py_ssize_t pos = 0;
286
430k
    while (pos < len) {
287
405k
        Py_ssize_t raw_line_len;
288
405k
        if (preserve_crlf) {
289
0
            const char *newline = memchr(data + pos, '\n', len - pos);
290
0
            raw_line_len = newline == NULL
291
0
                ? len - pos : newline - data - pos + 1;
292
0
        }
293
405k
        else {
294
405k
            raw_line_len = raw_line_length(data + pos, len - pos);
295
405k
        }
296
405k
        int terminated = preserve_crlf
297
405k
            ? data[pos + raw_line_len - 1] == '\n'
298
405k
            : data[pos + raw_line_len - 1] == '\n' ||
299
149k
              data[pos + raw_line_len - 1] == '\r';
300
405k
        int add_newline = add_final_newline &&
301
397k
            pos + raw_line_len == len && !terminated;
302
405k
        const char *line = data + pos;
303
        // raw_line_length stops at the first CR or LF, so any CR is in
304
        // the line terminator.
305
405k
        Py_ssize_t terminator_len = 0;
306
405k
        if (!preserve_crlf) {
307
405k
            if (line[raw_line_len - 1] == '\r') {
308
124k
                terminator_len = 1;
309
124k
            }
310
280k
            else if (raw_line_len > 1 && line[raw_line_len - 2] == '\r') {
311
296
                terminator_len = 2;
312
296
            }
313
405k
        }
314
405k
        _PyTok_Off appended;
315
405k
        if (terminator_len || add_newline) {
316
147k
            appended = _PyTok_SourceAppendLineWithNewline(
317
147k
                &tok->source, line, raw_line_len - terminator_len);
318
147k
        }
319
257k
        else {
320
257k
            appended = _PyTok_SourceAppendLine(&tok->source, line, raw_line_len);
321
257k
        }
322
405k
        if (appended < 0) {
323
0
            tok->done = PyErr_ExceptionMatches(PyExc_MemoryError)
324
0
                ? E_NOMEM : E_ERROR;
325
0
            return -1;
326
0
        }
327
405k
        tok->reader->prepared_final_newline = add_newline;
328
405k
        pos += raw_line_len;
329
405k
    }
330
    // Partial input includes the newline submitted by the next Enter key.
331
    // A final CR pairs with that newline instead of starting a blank line.
332
24.9k
    if (add_final_newline == _PyTokenizer_APPEND_NEWLINE &&
333
17.3k
        !tok->reader->prepared_final_newline &&
334
278
        (len == 0 || data[len - 1] != '\r'))
335
202
    {
336
202
        if (_PyTok_SourceAppendLine(&tok->source, "\n", 1) < 0) {
337
0
            return -1;
338
0
        }
339
202
        tok->reader->prepared_final_newline = _PyTokenizer_APPEND_NEWLINE;
340
202
    }
341
24.9k
    return 0;
342
24.9k
}
343
344
int
345
_PyTok_PrepareString(struct tok_state *tok, const char *input, int utf8_only,
346
                     int exec_input, int preserve_crlf)
347
26.4k
{
348
26.4k
    Py_ssize_t raw_len = strlen(input);
349
26.4k
    char *raw = (char *)input;
350
351
26.4k
    if (utf8_only) {
352
11.7k
        if (_PyTok_SetEncoding(tok, "utf-8") < 0) {
353
0
            return -1;
354
0
        }
355
11.7k
    }
356
14.6k
    else {
357
14.6k
        Py_ssize_t first_original_len = raw_line_length(raw, raw_len);
358
14.6k
        _PyTok_Chunk first = {
359
14.6k
            .data = raw,
360
14.6k
            .len = first_original_len,
361
14.6k
            .ownership = _PYTOK_CHUNK_BORROWED,
362
14.6k
        };
363
14.6k
        _PyTok_Chunk second = {0};
364
14.6k
        int have_second = first_original_len < raw_len;
365
14.6k
        if (have_second) {
366
5.20k
            second.data = raw + first_original_len;
367
5.20k
            second.len = raw_line_length(second.data,
368
5.20k
                                         raw_len - first_original_len);
369
5.20k
        }
370
14.6k
        Py_ssize_t bom_len;
371
14.6k
        _PyTok_EncodingResult detection = _PyTok_DetectEncoding(
372
14.6k
            tok, &first, have_second ? &second : NULL, 1, &bom_len);
373
14.6k
        if (detection == _PYTOK_ENCODING_ERROR) {
374
7
            return -1;
375
7
        }
376
14.6k
        raw += bom_len;
377
14.6k
        raw_len -= bom_len;
378
14.6k
    }
379
380
26.4k
    _PyTok_Chunk decoded = {
381
26.4k
        .data = raw,
382
26.4k
        .len = raw_len,
383
26.4k
        .ownership = _PYTOK_CHUNK_BORROWED,
384
26.4k
    };
385
26.4k
    const char *encoding = tok->reader->encoding;
386
26.4k
    if (encoding != NULL && strcmp(encoding, "utf-8") != 0) {
387
4.67k
        if (_PyTok_DecodeOnce(
388
4.67k
                tok, &decoded, encoding, NULL) < 0) {
389
1.49k
            return -1;
390
1.49k
        }
391
4.67k
    }
392
393
24.9k
    int stored = store_prepared_source(
394
24.9k
        tok, decoded.data, decoded.len, preserve_crlf, exec_input);
395
24.9k
    _PyTok_ChunkClear(&decoded);
396
24.9k
    if (stored < 0) {
397
0
        return -1;
398
0
    }
399
24.9k
    if (!utf8_only &&
400
13.1k
            (encoding == NULL || strcmp(encoding, "utf-8") == 0) &&
401
10.0k
            !_PyTokenizer_ensure_utf8(_PyTok_SourceData(&tok->source), tok, 1)) {
402
155
        return -1;
403
155
    }
404
24.7k
    return 0;
405
24.9k
}
406
407
int
408
_PyTok_StartDecoder(struct tok_state *tok, const char *errors)
409
0
{
410
0
    _PyTok_Reader *reader = tok->reader;
411
0
    if (reader->encoding == NULL || reader->decoder != NULL) {
412
0
        return 0;
413
0
    }
414
0
    if (reader->kind == _PYTOK_READER_FILE &&
415
0
            strcmp(reader->encoding, "utf-8") == 0) {
416
0
        return 0;
417
0
    }
418
419
0
    PyObject *codec = _PyCodec_LookupTextEncoding(reader->encoding, NULL);
420
0
    if (codec != NULL) {
421
0
        reader->decoder = _PyCodecInfo_GetIncrementalDecoder(codec, errors);
422
0
        Py_DECREF(codec);
423
0
    }
424
0
    if (reader->decoder == NULL) {
425
0
        tok->done = PyErr_ExceptionMatches(PyExc_MemoryError)
426
0
            ? E_NOMEM : E_DECODE;
427
0
        if (reader->kind == _PYTOK_READER_FILE) {
428
0
            _PyTokenizer_raise_init_error(
429
0
                tok->filename != NULL ? tok->filename : Py_None);
430
0
        }
431
0
        return -1;
432
0
    }
433
0
    return 0;
434
0
}
435
436
int
437
_PyTok_DecodeChunk(struct tok_state *tok, _PyTok_Chunk *chunk, int final)
438
0
{
439
0
    _PyTok_Reader *reader = tok->reader;
440
0
    if (reader->decoder == NULL) {
441
0
        return 0;
442
0
    }
443
0
    int strip_bom = reader->kind == _PYTOK_READER_READLINE &&
444
0
        chunk->len >= UTF16_BOM_LENGTH &&
445
0
        (memcmp(chunk->data, UTF16_LE_BOM, UTF16_BOM_LENGTH) == 0 ||
446
0
         memcmp(chunk->data, UTF16_BE_BOM, UTF16_BOM_LENGTH) == 0);
447
0
    PyObject *input;
448
0
    if (chunk->ownership == _PYTOK_CHUNK_PYOBJECT &&
449
0
            PyBytes_Check(chunk->owner) &&
450
0
            chunk->data == PyBytes_AS_STRING(chunk->owner)) {
451
0
        input = Py_NewRef(chunk->owner);
452
0
    }
453
0
    else {
454
0
        input = PyBytes_FromStringAndSize(chunk->data, chunk->len);
455
0
    }
456
0
    if (input == NULL) {
457
0
        tok->done = E_NOMEM;
458
0
        return -1;
459
0
    }
460
0
    PyObject *unicode = PyObject_CallMethodObjArgs(
461
0
        reader->decoder, &_Py_ID(decode), input,
462
0
        final ? Py_True : Py_False, NULL);
463
0
    Py_DECREF(input);
464
0
    if (unicode == NULL) {
465
0
        tok->done = PyErr_ExceptionMatches(PyExc_MemoryError)
466
0
            ? E_NOMEM : E_DECODE;
467
0
        if (reader->kind == _PYTOK_READER_FILE) {
468
0
            _PyTokenizer_raise_init_error(
469
0
                tok->filename != NULL ? tok->filename : Py_None);
470
0
        }
471
0
        return -1;
472
0
    }
473
0
    if (!PyUnicode_Check(unicode)) {
474
0
        PyErr_Format(PyExc_TypeError,
475
0
                     "decoder should return a string result, not '%.200s'",
476
0
                     Py_TYPE(unicode)->tp_name);
477
0
        Py_DECREF(unicode);
478
0
        tok->done = E_DECODE;
479
0
        return -1;
480
0
    }
481
0
    return chunk_set_unicode(tok, chunk, unicode, strip_bom);
482
0
}
483
484
int
485
_PyTok_DecoderHasBufferedInput(struct tok_state *tok)
486
0
{
487
0
    if (tok->reader->decoder == NULL) {
488
0
        return 0;
489
0
    }
490
0
    PyObject *state = PyObject_CallMethodNoArgs(
491
0
        tok->reader->decoder, &_Py_ID(getstate));
492
0
    if (state == NULL) {
493
0
        tok->done = PyErr_ExceptionMatches(PyExc_MemoryError)
494
0
            ? E_NOMEM : E_DECODE;
495
0
        return -1;
496
0
    }
497
0
    if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) != 2 ||
498
0
            !PyBytes_Check(PyTuple_GET_ITEM(state, 0)) ||
499
0
            !PyLong_Check(PyTuple_GET_ITEM(state, 1))) {
500
0
        Py_DECREF(state);
501
0
        PyErr_SetString(PyExc_TypeError,
502
0
                        "incremental decoder getstate() must return (bytes, int)");
503
0
        tok->done = E_DECODE;
504
0
        return -1;
505
0
    }
506
0
    int pending = PyBytes_GET_SIZE(PyTuple_GET_ITEM(state, 0)) != 0;
507
0
    Py_DECREF(state);
508
0
    return pending;
509
0
}