Coverage Report

Created: 2026-08-13 06:33

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