Coverage Report

Created: 2025-08-26 06:26

/src/cpython/Parser/tokenizer/string_tokenizer.c
Line
Count
Source (jump to first uncovered line)
1
#include "Python.h"
2
#include "errcode.h"
3
4
#include "helpers.h"
5
#include "../lexer/state.h"
6
7
static int
8
245k
tok_underflow_string(struct tok_state *tok) {
9
245k
    char *end = strchr(tok->inp, '\n');
10
245k
    if (end != NULL) {
11
227k
        end++;
12
227k
    }
13
17.4k
    else {
14
17.4k
        end = strchr(tok->inp, '\0');
15
17.4k
        if (end == tok->inp) {
16
17.2k
            tok->done = E_EOF;
17
17.2k
            return 0;
18
17.2k
        }
19
17.4k
    }
20
228k
    if (tok->start == NULL) {
21
211k
        tok->buf = tok->cur;
22
211k
    }
23
228k
    tok->line_start = tok->cur;
24
228k
    ADVANCE_LINENO();
25
228k
    tok->inp = end;
26
228k
    return 1;
27
245k
}
28
29
/* Fetch a byte from TOK, using the string buffer. */
30
static int
31
23.1k
buf_getc(struct tok_state *tok) {
32
23.1k
    return Py_CHARMASK(*tok->str++);
33
23.1k
}
34
35
/* Unfetch a byte from TOK, using the string buffer. */
36
static void
37
23.1k
buf_ungetc(int c, struct tok_state *tok) {
38
23.1k
    tok->str--;
39
23.1k
    assert(Py_CHARMASK(*tok->str) == c);        /* tok->cur may point to read-only segment */
40
23.1k
}
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
4.33k
buf_setreadl(struct tok_state *tok, const char* enc) {
46
4.33k
    tok->enc = enc;
47
4.33k
    return 1;
48
4.33k
}
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
23.0k
{
56
23.0k
    PyObject* utf8 = NULL;
57
23.0k
    char *str;
58
23.0k
    const char *s;
59
23.0k
    const char *newl[2] = {NULL, NULL};
60
23.0k
    int lineno = 0;
61
23.0k
    tok->input = str = _PyTokenizer_translate_newlines(input, single, preserve_crlf, tok);
62
23.0k
    if (str == NULL)
63
0
        return NULL;
64
23.0k
    tok->enc = NULL;
65
23.0k
    tok->str = str;
66
23.0k
    if (!_PyTokenizer_check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
67
0
        return _PyTokenizer_error_ret(tok);
68
23.0k
    str = tok->str;             /* string after BOM if any */
69
23.0k
    assert(str);
70
23.0k
    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
2.09M
    for (s = str;; s++) {
77
2.09M
        if (*s == '\0') break;
78
2.07M
        else if (*s == '\n') {
79
29.0k
            assert(lineno < 2);
80
29.0k
            newl[lineno] = s;
81
29.0k
            lineno++;
82
29.0k
            if (lineno == 2) break;
83
29.0k
        }
84
2.09M
    }
85
23.0k
    tok->enc = NULL;
86
    /* need to check line 1 and 2 separately since check_coding_spec
87
       assumes a single line as input */
88
23.0k
    if (newl[0]) {
89
23.0k
        if (!_PyTokenizer_check_coding_spec(str, newl[0] - str, tok, buf_setreadl)) {
90
2
            return NULL;
91
2
        }
92
23.0k
        if (tok->enc == NULL && tok->decoding_state != STATE_NORMAL && newl[1]) {
93
834
            if (!_PyTokenizer_check_coding_spec(newl[0]+1, newl[1] - newl[0],
94
834
                                   tok, buf_setreadl))
95
1
                return NULL;
96
834
        }
97
23.0k
    }
98
23.0k
    if (tok->enc != NULL) {
99
4.33k
        assert(utf8 == NULL);
100
4.33k
        utf8 = _PyTokenizer_translate_into_utf8(str, tok->enc);
101
4.33k
        if (utf8 == NULL)
102
1.78k
            return _PyTokenizer_error_ret(tok);
103
2.55k
        str = PyBytes_AS_STRING(utf8);
104
2.55k
    }
105
21.2k
    assert(tok->decoding_buffer == NULL);
106
21.2k
    tok->decoding_buffer = utf8; /* CAUTION */
107
21.2k
    return str;
108
23.0k
}
109
110
/* Set up tokenizer for string */
111
struct tok_state *
112
_PyTokenizer_FromString(const char *str, int exec_input, int preserve_crlf)
113
23.0k
{
114
23.0k
    struct tok_state *tok = _PyTokenizer_tok_new();
115
23.0k
    char *decoded;
116
117
23.0k
    if (tok == NULL)
118
0
        return NULL;
119
23.0k
    decoded = decode_str(str, exec_input, tok, preserve_crlf);
120
23.0k
    if (decoded == NULL) {
121
1.79k
        _PyTokenizer_Free(tok);
122
1.79k
        return NULL;
123
1.79k
    }
124
125
21.2k
    tok->buf = tok->cur = tok->inp = decoded;
126
21.2k
    tok->end = decoded;
127
21.2k
    tok->underflow = &tok_underflow_string;
128
21.2k
    return tok;
129
23.0k
}