Coverage Report

Created: 2026-11-30 07:19

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_token.h"
3
#include "errcode.h"
4
5
#include "state.h"
6
#include "../tokenizer/helpers.h"
7
#include "../tokenizer/reader.h"
8
9
ftstring_state *
10
_PyLexer_PushFTString(struct tok_state *tok)
11
23.9k
{
12
23.9k
    assert(tok->ftstring_depth >= 0 && tok->ftstring_depth <= tok->ftstring_capacity);
13
23.9k
    int next_depth = tok->ftstring_depth + 1;
14
23.9k
    if (next_depth >= MAXFTSTRINGLEVEL) {
15
1
        _PyTokenizer_syntaxerror(
16
1
            tok, "too many nested f-strings or t-strings");
17
1
        return NULL;
18
1
    }
19
23.9k
    if (tok->ftstring_depth == tok->ftstring_capacity) {
20
952
        int capacity = Py_MIN(Py_MAX(tok->ftstring_capacity * 2, 4),
21
952
                              MAXFTSTRINGLEVEL);
22
952
        size_t size = (size_t)capacity * sizeof(*tok->ftstring_stack);
23
952
        ftstring_state *stack;
24
952
        if (tok->ftstring_stack == tok->ftstring_stack_inline) {
25
381
            stack = PyMem_Malloc(size);
26
381
            if (stack != NULL) {
27
381
                memcpy(stack, tok->ftstring_stack,
28
381
                       (size_t)tok->ftstring_depth * sizeof(*stack));
29
381
            }
30
381
        }
31
571
        else {
32
571
            stack = PyMem_Realloc(tok->ftstring_stack, size);
33
571
        }
34
952
        if (stack == NULL) {
35
0
            PyErr_NoMemory();
36
0
            tok->done = E_NOMEM;
37
0
            return NULL;
38
0
        }
39
952
        tok->ftstring_stack = stack;
40
952
        tok->ftstring_capacity = capacity;
41
952
    }
42
23.9k
    ftstring_state *state = &tok->ftstring_stack[tok->ftstring_depth];
43
23.9k
    tok->ftstring_depth = next_depth;
44
23.9k
    *state = (ftstring_state){0};
45
23.9k
    return state;
46
23.9k
}
47
48
void
49
_PyLexer_PopFTString(struct tok_state *tok)
50
16.1k
{
51
16.1k
    ftstring_state *state = _PyLexer_CurrentFTString(tok);
52
16.1k
    assert(state != NULL);
53
16.1k
    PyMem_Free(state->comments);
54
16.1k
    tok->ftstring_depth--;
55
16.1k
}
56
57
/* Free a tok_state structure */
58
void
59
_PyTokenizer_Free(struct tok_state *tok)
60
31.7k
{
61
31.7k
    Py_XDECREF(tok->filename);
62
31.7k
    Py_XDECREF(tok->module);
63
31.7k
    _PyTok_ReaderFree(tok);
64
31.7k
    _PyTok_SourceClear(&tok->source);
65
39.5k
    for (int i = 0; i < tok->ftstring_depth; i++) {
66
7.78k
        PyMem_Free(tok->ftstring_stack[i].comments);
67
7.78k
    }
68
31.7k
    if (tok->ftstring_stack != tok->ftstring_stack_inline) {
69
381
        PyMem_Free(tok->ftstring_stack);
70
381
    }
71
31.7k
    PyMem_Free(tok);
72
31.7k
}
73
74
void
75
6.03M
_PyToken_Init(struct token *token) {
76
6.03M
    *token = (struct token){
77
6.03M
        .type = -1,
78
6.03M
        .span = {-1, -1},
79
6.03M
        .start_loc = {-1, -1},
80
6.03M
        .end_loc = {-1, -1},
81
6.03M
    };
82
6.03M
}
83
84
int
85
_PyLexer_token_setup(struct tok_state *tok, struct token *token, int type, _PyTok_Off start, _PyTok_Off end)
86
6.41M
{
87
6.41M
    token->level = tok->level;
88
6.41M
    token->is_raw = 0;
89
6.41M
    token->is_incomplete = 0;
90
6.41M
    assert((start == -1 && end == -1) || (start >= 0 && end >= start));
91
6.41M
    token->span = (_PyTok_Span){start, end};
92
6.41M
    if (start >= 0) {
93
6.34M
        token->start_loc = tok->start_loc;
94
6.34M
        token->end_loc = (_PyTok_Loc){tok->lineno, _PyLexer_ByteColumn(tok)};
95
6.34M
    }
96
68.5k
    else {
97
68.5k
        token->start_loc = token->end_loc = (_PyTok_Loc){tok->lineno, -1};
98
68.5k
    }
99
6.41M
    return type;
100
6.41M
}