Coverage Report

Created: 2026-06-21 06:15

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