Coverage Report

Created: 2026-01-17 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/Parser/tokenizer/string_tokenizer.c
Line
Count
Source
1
#include "Python.h"
2
#include "errcode.h"
3
4
#include "helpers.h"
5
#include "../lexer/state.h"
6
7
static int
8
368k
tok_underflow_string(struct tok_state *tok) {
9
368k
    char *end = strchr(tok->inp, '\n');
10
368k
    if (end != NULL) {
11
334k
        end++;
12
334k
    }
13
33.8k
    else {
14
33.8k
        end = strchr(tok->inp, '\0');
15
33.8k
        if (end == tok->inp) {
16
23.1k
            tok->done = E_EOF;
17
23.1k
            return 0;
18
23.1k
        }
19
33.8k
    }
20
345k
    if (tok->start == NULL) {
21
274k
        tok->buf = tok->cur;
22
274k
    }
23
345k
    tok->line_start = tok->cur;
24
345k
    ADVANCE_LINENO();
25
345k
    tok->inp = end;
26
345k
    return 1;
27
368k
}
28
29
/* Fetch a byte from TOK, using the string buffer. */
30
static int
31
26.7k
buf_getc(struct tok_state *tok) {
32
26.7k
    return Py_CHARMASK(*tok->str++);
33
26.7k
}
34
35
/* Unfetch a byte from TOK, using the string buffer. */
36
static void
37
26.7k
buf_ungetc(int c, struct tok_state *tok) {
38
26.7k
    tok->str--;
39
26.7k
    assert(Py_CHARMASK(*tok->str) == c);        /* tok->cur may point to read-only segment */
40
26.7k
}
41
42
/* Set the readline function for TOK to ENC. For the string-based
43
   tokenizer, this means to just record the encoding. */
44
static int
45
3.24k
buf_setreadl(struct tok_state *tok, const char* enc) {
46
3.24k
    tok->enc = enc;
47
3.24k
    return 1;
48
3.24k
}
49
50
/* Decode a byte string STR for use as the buffer of TOK.
51
   Look for encoding declarations inside STR, and record them
52
   inside TOK.  */
53
static char *
54
decode_str(const char *input, int single, struct tok_state *tok, int preserve_crlf)
55
26.6k
{
56
26.6k
    PyObject* utf8 = NULL;
57
26.6k
    char *str;
58
26.6k
    const char *s;
59
26.6k
    const char *newl[2] = {NULL, NULL};
60
26.6k
    int lineno = 0;
61
26.6k
    tok->input = str = _PyTokenizer_translate_newlines(input, single, preserve_crlf, tok);
62
26.6k
    if (str == NULL)
63
0
        return NULL;
64
26.6k
    tok->enc = NULL;
65
26.6k
    tok->str = str;
66
26.6k
    if (!_PyTokenizer_check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
67
0
        return _PyTokenizer_error_ret(tok);
68
26.6k
    str = tok->str;             /* string after BOM if any */
69
26.6k
    assert(str);
70
26.6k
    if (tok->enc != NULL) {
71
0
        utf8 = _PyTokenizer_translate_into_utf8(str, tok->enc);
72
0
        if (utf8 == NULL)
73
0
            return _PyTokenizer_error_ret(tok);
74
0
        str = PyBytes_AsString(utf8);
75
0
    }
76
7.34M
    for (s = str;; s++) {
77
7.34M
        if (*s == '\0') break;
78
7.33M
        else if (*s == '\n') {
79
25.8k
            assert(lineno < 2);
80
25.8k
            newl[lineno] = s;
81
25.8k
            lineno++;
82
25.8k
            if (lineno == 2) break;
83
25.8k
        }
84
7.34M
    }
85
26.6k
    tok->enc = NULL;
86
    /* need to check line 1 and 2 separately since check_coding_spec
87
       assumes a single line as input */
88
26.6k
    if (newl[0]) {
89
17.0k
        tok->lineno = 1;
90
17.0k
        if (!_PyTokenizer_check_coding_spec(str, newl[0] - str, tok, buf_setreadl)) {
91
1
            return NULL;
92
1
        }
93
17.0k
        if (tok->enc == NULL && tok->decoding_state != STATE_NORMAL && newl[1]) {
94
690
            tok->lineno = 2;
95
690
            if (!_PyTokenizer_check_coding_spec(newl[0]+1, newl[1] - newl[0],
96
690
                                   tok, buf_setreadl))
97
0
                return NULL;
98
690
        }
99
17.0k
    }
100
26.6k
    tok->lineno = 0;
101
26.6k
    if (tok->enc != NULL) {
102
3.24k
        assert(utf8 == NULL);
103
3.24k
        utf8 = _PyTokenizer_translate_into_utf8(str, tok->enc);
104
3.24k
        if (utf8 == NULL)
105
976
            return _PyTokenizer_error_ret(tok);
106
2.26k
        str = PyBytes_AS_STRING(utf8);
107
2.26k
    }
108
23.4k
    else if (!_PyTokenizer_ensure_utf8(str, tok, 1)) {
109
606
        return _PyTokenizer_error_ret(tok);
110
606
    }
111
26.6k
    assert(tok->decoding_buffer == NULL);
112
25.1k
    tok->decoding_buffer = utf8; /* CAUTION */
113
25.1k
    return str;
114
25.1k
}
115
116
/* Set up tokenizer for string */
117
struct tok_state *
118
_PyTokenizer_FromString(const char *str, int exec_input, int preserve_crlf)
119
26.6k
{
120
26.6k
    struct tok_state *tok = _PyTokenizer_tok_new();
121
26.6k
    char *decoded;
122
123
26.6k
    if (tok == NULL)
124
0
        return NULL;
125
26.6k
    decoded = decode_str(str, exec_input, tok, preserve_crlf);
126
26.6k
    if (decoded == NULL) {
127
1.58k
        _PyTokenizer_Free(tok);
128
1.58k
        return NULL;
129
1.58k
    }
130
131
25.1k
    tok->buf = tok->cur = tok->inp = decoded;
132
25.1k
    tok->end = decoded;
133
25.1k
    tok->underflow = &tok_underflow_string;
134
25.1k
    return tok;
135
26.6k
}