Coverage Report

Created: 2026-06-09 06:53

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