Coverage Report

Created: 2026-08-31 07:18

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
#include "../tokenizer/reader.h"
8
9
/* Never change this */
10
24.5k
#define TABSIZE 8
11
12
/* Create and initialize a new tok_state structure */
13
struct tok_state *
14
_PyTokenizer_tok_new(void)
15
24.5k
{
16
24.5k
    struct tok_state *tok = (struct tok_state *)PyMem_Calloc(
17
24.5k
                                            1,
18
24.5k
                                            sizeof(struct tok_state));
19
24.5k
    if (tok == NULL) {
20
0
        PyErr_NoMemory();
21
0
        return NULL;
22
0
    }
23
24
24.5k
    tok->buf = tok->cur = tok->inp = NULL;
25
24.5k
    tok->fp_interactive = 0;
26
24.5k
    tok->interactive_src_start = NULL;
27
24.5k
    tok->interactive_src_end = NULL;
28
24.5k
    tok->start = NULL;
29
24.5k
    tok->end = NULL;
30
24.5k
    tok->done = E_OK;
31
24.5k
    tok->fp = 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 = 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->input_error = 0;
44
24.5k
    tok->encoding = NULL;
45
24.5k
    tok->filename = NULL;
46
24.5k
    tok->module = NULL;
47
24.5k
    tok->type_comments = 0;
48
24.5k
    tok->interactive_underflow = IUNDERFLOW_NORMAL;
49
24.5k
    tok->str = NULL;
50
24.5k
    tok->report_warnings = 1;
51
24.5k
    tok->tok_extra_tokens = 0;
52
24.5k
    tok->comment_newline = 0;
53
24.5k
    tok->implicit_newline = 0;
54
24.5k
    _PyTok_SourceInit(&tok->source);
55
24.5k
    tok->reader = NULL;
56
24.5k
    tok->tok_mode_stack[0] = (tokenizer_mode){.kind =TOK_REGULAR_MODE, .quote='\0', .quote_size = 0, .in_debug=0};
57
24.5k
    tok->tok_mode_stack_index = 0;
58
#ifdef Py_DEBUG
59
    tok->debug = _Py_GetConfig()->parser_debug;
60
#endif
61
24.5k
    return tok;
62
24.5k
}
63
64
static void
65
free_fstring_expressions(struct tok_state *tok)
66
24.5k
{
67
24.5k
    int index;
68
24.5k
    tokenizer_mode *mode;
69
70
57.0k
    for (index = tok->tok_mode_stack_index; index >= 0; --index) {
71
32.4k
        mode = &(tok->tok_mode_stack[index]);
72
32.4k
        if (mode->last_expr_buffer != NULL) {
73
7.70k
            PyMem_Free(mode->last_expr_buffer);
74
7.70k
            mode->last_expr_buffer = NULL;
75
7.70k
            mode->last_expr_size = 0;
76
7.70k
            mode->last_expr_end = -1;
77
7.70k
            mode->in_format_spec = 0;
78
7.70k
        }
79
32.4k
    }
80
24.5k
}
81
82
/* Free a tok_state structure */
83
void
84
_PyTokenizer_Free(struct tok_state *tok)
85
24.5k
{
86
24.5k
    if (tok->encoding != NULL) {
87
15.8k
        PyMem_Free(tok->encoding);
88
15.8k
    }
89
24.5k
    Py_XDECREF(tok->filename);
90
24.5k
    Py_XDECREF(tok->module);
91
24.5k
    _PyTok_ReaderFree(tok);
92
24.5k
    _PyTok_SourceClear(&tok->source);
93
24.5k
    free_fstring_expressions(tok);
94
24.5k
    PyMem_Free(tok);
95
24.5k
}
96
97
void
98
5.72k
_PyToken_Free(struct token *token) {
99
5.72k
    Py_XDECREF(token->metadata);
100
5.72k
}
101
102
void
103
5.05M
_PyToken_Init(struct token *token) {
104
5.05M
    token->metadata = NULL;
105
5.05M
}
106
107
int
108
_PyLexer_type_comment_token_setup(struct tok_state *tok, struct token *token, int type, int col_offset,
109
                         int end_col_offset, const char *start, const char *end)
110
2.37k
{
111
2.37k
    token->level = tok->level;
112
2.37k
    token->lineno = token->end_lineno = tok->lineno;
113
2.37k
    token->col_offset = col_offset;
114
2.37k
    token->end_col_offset = end_col_offset;
115
2.37k
    token->start = start;
116
2.37k
    token->end = end;
117
2.37k
    return type;
118
2.37k
}
119
120
int
121
_PyLexer_token_setup(struct tok_state *tok, struct token *token, int type, const char *start, const char *end)
122
5.22M
{
123
5.22M
    assert((start == NULL && end == NULL) || (start != NULL && end != NULL));
124
5.22M
    token->level = tok->level;
125
5.22M
    if (ISSTRINGLIT(type)) {
126
126k
        token->lineno = tok->first_lineno;
127
126k
    }
128
5.09M
    else {
129
5.09M
        token->lineno = tok->lineno;
130
5.09M
    }
131
5.22M
    token->end_lineno = tok->lineno;
132
5.22M
    token->col_offset = token->end_col_offset = -1;
133
5.22M
    token->start = start;
134
5.22M
    token->end = end;
135
136
5.22M
    if (start != NULL && end != NULL) {
137
5.16M
        token->col_offset = tok->starting_col_offset;
138
5.16M
        token->end_col_offset = tok->col_offset;
139
5.16M
    }
140
5.22M
    return type;
141
5.22M
}