Coverage Report

Created: 2026-06-09 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/Parser/lexer/state.c
Line
Count
Source
1
#include "Python.h"
2
#include "pycore_pystate.h"
3
#include "pycore_token.h"
4
#include "errcode.h"
5
6
#include "state.h"
7
8
/* Never change this */
9
15.1k
#define TABSIZE 8
10
11
/* Create and initialize a new tok_state structure */
12
struct tok_state *
13
_PyTokenizer_tok_new(void)
14
15.1k
{
15
15.1k
    struct tok_state *tok = (struct tok_state *)PyMem_Calloc(
16
15.1k
                                            1,
17
15.1k
                                            sizeof(struct tok_state));
18
15.1k
    if (tok == NULL) {
19
0
        PyErr_NoMemory();
20
0
        return NULL;
21
0
    }
22
23
15.1k
    tok->buf = tok->cur = tok->inp = NULL;
24
15.1k
    tok->fp_interactive = 0;
25
15.1k
    tok->interactive_src_start = NULL;
26
15.1k
    tok->interactive_src_end = NULL;
27
15.1k
    tok->start = NULL;
28
15.1k
    tok->end = NULL;
29
15.1k
    tok->done = E_OK;
30
15.1k
    tok->fp = NULL;
31
15.1k
    tok->input = NULL;
32
15.1k
    tok->tabsize = TABSIZE;
33
15.1k
    tok->indent = 0;
34
15.1k
    tok->indstack[0] = 0;
35
15.1k
    tok->atbol = 1;
36
15.1k
    tok->pendin = 0;
37
15.1k
    tok->prompt = tok->nextprompt = NULL;
38
15.1k
    tok->lineno = 0;
39
15.1k
    tok->starting_col_offset = -1;
40
15.1k
    tok->col_offset = -1;
41
15.1k
    tok->level = 0;
42
15.1k
    tok->altindstack[0] = 0;
43
15.1k
    tok->decoding_state = STATE_INIT;
44
15.1k
    tok->decoding_erred = 0;
45
15.1k
    tok->enc = NULL;
46
15.1k
    tok->encoding = NULL;
47
15.1k
    tok->cont_line = 0;
48
15.1k
    tok->filename = NULL;
49
15.1k
    tok->module = NULL;
50
15.1k
    tok->decoding_readline = NULL;
51
15.1k
    tok->decoding_buffer = NULL;
52
15.1k
    tok->readline = NULL;
53
15.1k
    tok->type_comments = 0;
54
15.1k
    tok->interactive_underflow = IUNDERFLOW_NORMAL;
55
15.1k
    tok->underflow = NULL;
56
15.1k
    tok->str = NULL;
57
15.1k
    tok->report_warnings = 1;
58
15.1k
    tok->tok_extra_tokens = 0;
59
15.1k
    tok->comment_newline = 0;
60
15.1k
    tok->implicit_newline = 0;
61
15.1k
    tok->tok_mode_stack[0] = (tokenizer_mode){.kind =TOK_REGULAR_MODE, .quote='\0', .quote_size = 0, .in_debug=0};
62
15.1k
    tok->tok_mode_stack_index = 0;
63
#ifdef Py_DEBUG
64
    tok->debug = _Py_GetConfig()->parser_debug;
65
#endif
66
15.1k
    return tok;
67
15.1k
}
68
69
static void
70
free_fstring_expressions(struct tok_state *tok)
71
15.1k
{
72
15.1k
    int index;
73
15.1k
    tokenizer_mode *mode;
74
75
36.7k
    for (index = tok->tok_mode_stack_index; index >= 0; --index) {
76
21.5k
        mode = &(tok->tok_mode_stack[index]);
77
21.5k
        if (mode->last_expr_buffer != NULL) {
78
6.26k
            PyMem_Free(mode->last_expr_buffer);
79
6.26k
            mode->last_expr_buffer = NULL;
80
6.26k
            mode->last_expr_size = 0;
81
6.26k
            mode->last_expr_end = -1;
82
6.26k
            mode->in_format_spec = 0;
83
6.26k
        }
84
21.5k
    }
85
15.1k
}
86
87
/* Free a tok_state structure */
88
void
89
_PyTokenizer_Free(struct tok_state *tok)
90
15.1k
{
91
15.1k
    if (tok->encoding != NULL) {
92
8.78k
        PyMem_Free(tok->encoding);
93
8.78k
    }
94
15.1k
    Py_XDECREF(tok->decoding_readline);
95
15.1k
    Py_XDECREF(tok->decoding_buffer);
96
15.1k
    Py_XDECREF(tok->readline);
97
15.1k
    Py_XDECREF(tok->filename);
98
15.1k
    Py_XDECREF(tok->module);
99
15.1k
    if ((tok->readline != NULL || tok->fp != NULL ) && tok->buf != NULL) {
100
0
        PyMem_Free(tok->buf);
101
0
    }
102
15.1k
    if (tok->input) {
103
15.1k
        PyMem_Free(tok->input);
104
15.1k
    }
105
15.1k
    if (tok->interactive_src_start != NULL) {
106
0
        PyMem_Free(tok->interactive_src_start);
107
0
    }
108
15.1k
    free_fstring_expressions(tok);
109
15.1k
    PyMem_Free(tok);
110
15.1k
}
111
112
void
113
4.26k
_PyToken_Free(struct token *token) {
114
4.26k
    Py_XDECREF(token->metadata);
115
4.26k
}
116
117
void
118
1.95M
_PyToken_Init(struct token *token) {
119
1.95M
    token->metadata = NULL;
120
1.95M
}
121
122
int
123
_PyLexer_type_comment_token_setup(struct tok_state *tok, struct token *token, int type, int col_offset,
124
                         int end_col_offset, const char *start, const char *end)
125
80
{
126
80
    token->level = tok->level;
127
80
    token->lineno = token->end_lineno = tok->lineno;
128
80
    token->col_offset = col_offset;
129
80
    token->end_col_offset = end_col_offset;
130
80
    token->start = start;
131
80
    token->end = end;
132
80
    return type;
133
80
}
134
135
int
136
_PyLexer_token_setup(struct tok_state *tok, struct token *token, int type, const char *start, const char *end)
137
2.11M
{
138
2.11M
    assert((start == NULL && end == NULL) || (start != NULL && end != NULL));
139
2.11M
    token->level = tok->level;
140
2.11M
    if (ISSTRINGLIT(type)) {
141
43.0k
        token->lineno = tok->first_lineno;
142
43.0k
    }
143
2.06M
    else {
144
2.06M
        token->lineno = tok->lineno;
145
2.06M
    }
146
2.11M
    token->end_lineno = tok->lineno;
147
2.11M
    token->col_offset = token->end_col_offset = -1;
148
2.11M
    token->start = start;
149
2.11M
    token->end = end;
150
151
2.11M
    if (start != NULL && end != NULL) {
152
2.08M
        token->col_offset = tok->starting_col_offset;
153
2.08M
        token->end_col_offset = tok->col_offset;
154
2.08M
    }
155
2.11M
    return type;
156
2.11M
}