Coverage Report

Created: 2026-11-30 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/Python/future.c
Line
Count
Source
1
#include "Python.h"
2
#include "pycore_ast.h"           // _PyAST_GetDocString()
3
#include "pycore_pyerrors.h"      // _Py_CalculateSuggestions()
4
#include "pycore_symtable.h"      // _PyFutureFeatures
5
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
6
7
103
#define UNDEFINED_FUTURE_FEATURE "future feature '%.100s' is not defined"
8
9
static int
10
future_check_features(_PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
11
2.20k
{
12
2.20k
    Py_ssize_t i;
13
14
2.20k
    assert(s->kind == ImportFrom_kind);
15
16
2.20k
    asdl_alias_seq *names = s->v.ImportFrom.names;
17
4.30k
    for (i = 0; i < asdl_seq_LEN(names); i++) {
18
2.20k
        alias_ty name = (alias_ty)asdl_seq_GET(names, i);
19
2.20k
        const char *feature = PyUnicode_AsUTF8(name->name);
20
2.20k
        if (!feature)
21
0
            return 0;
22
2.20k
        if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
23
36
            continue;
24
2.17k
        } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
25
70
            continue;
26
2.10k
        } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
27
69
            continue;
28
2.03k
        } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
29
1
            continue;
30
2.03k
        } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
31
1
            continue;
32
2.03k
        } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
33
1
            continue;
34
2.02k
        } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
35
1
            continue;
36
2.02k
        } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
37
11
            ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
38
2.01k
        } else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
39
1
            continue;
40
2.01k
        } else if (strcmp(feature, FUTURE_ANNOTATIONS) == 0) {
41
1.91k
            ff->ff_features |= CO_FUTURE_ANNOTATIONS;
42
1.91k
        } else if (strcmp(feature, "braces") == 0) {
43
1
            PyErr_SetString(PyExc_SyntaxError,
44
1
                            "not a chance");
45
1
            PyErr_RangedSyntaxLocationObject(filename,
46
1
                                             name->lineno,
47
1
                                             name->col_offset + 1,
48
1
                                             name->end_lineno,
49
1
                                             name->end_col_offset + 1);
50
1
            return 0;
51
103
        } else {
52
            // Keep this list in sync with the feature checks above.
53
103
            PyObject *future_features = Py_BuildValue("[ssssssssss]",
54
103
                                                      FUTURE_NESTED_SCOPES,
55
103
                                                      FUTURE_GENERATORS,
56
103
                                                      FUTURE_DIVISION,
57
103
                                                      FUTURE_ABSOLUTE_IMPORT,
58
103
                                                      FUTURE_WITH_STATEMENT,
59
103
                                                      FUTURE_PRINT_FUNCTION,
60
103
                                                      FUTURE_UNICODE_LITERALS,
61
103
                                                      FUTURE_BARRY_AS_BDFL,
62
103
                                                      FUTURE_GENERATOR_STOP,
63
103
                                                      FUTURE_ANNOTATIONS);
64
103
            PyObject *suggestion = NULL;
65
103
            if (future_features != NULL) {
66
103
                suggestion = _Py_CalculateSuggestions(future_features,
67
103
                                                      name->name);
68
103
            }
69
103
            if (suggestion != NULL) {
70
31
                PyErr_Format(PyExc_SyntaxError,
71
31
                             UNDEFINED_FUTURE_FEATURE ". Did you mean: %R?",
72
31
                             feature, suggestion);
73
31
                Py_DECREF(suggestion);
74
31
            }
75
72
            else {
76
                // Do not fail on missing suggestion,
77
                // just show the default message.
78
72
                PyErr_Format(PyExc_SyntaxError,
79
72
                             UNDEFINED_FUTURE_FEATURE,
80
72
                             feature);
81
72
            }
82
103
            Py_XDECREF(future_features);
83
103
            PyErr_RangedSyntaxLocationObject(filename,
84
103
                                             name->lineno,
85
103
                                             name->col_offset + 1,
86
103
                                             name->end_lineno,
87
103
                                             name->end_col_offset + 1);
88
103
            return 0;
89
103
        }
90
2.20k
    }
91
2.10k
    return 1;
92
2.20k
}
93
94
static int
95
future_parse(_PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
96
17.8k
{
97
17.8k
    if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) {
98
2.31k
        return 1;
99
2.31k
    }
100
101
15.5k
    Py_ssize_t n = asdl_seq_LEN(mod->v.Module.body);
102
15.5k
    if (n == 0) {
103
425
        return 1;
104
425
    }
105
106
15.1k
    Py_ssize_t i = 0;
107
15.1k
    if (_PyAST_GetDocString(mod->v.Module.body) != NULL) {
108
277
        i++;
109
277
    }
110
111
17.2k
    for (; i < n; i++) {
112
16.9k
        stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
113
114
        /* The only things that can precede a future statement
115
         *  are another future statement and a doc string.
116
         */
117
118
16.9k
        if (s->kind == ImportFrom_kind && s->v.ImportFrom.level == 0) {
119
2.28k
            identifier modname = s->v.ImportFrom.module;
120
2.28k
            if (modname &&
121
2.28k
                _PyUnicode_EqualToASCIIString(modname, "__future__")) {
122
2.20k
                if (!future_check_features(ff, s, filename)) {
123
104
                    return 0;
124
104
                }
125
2.10k
                ff->ff_location = SRC_LOCATION_FROM_AST(s);
126
2.10k
            }
127
77
            else {
128
77
                return 1;
129
77
            }
130
2.28k
        }
131
14.6k
        else {
132
14.6k
            return 1;
133
14.6k
        }
134
16.9k
    }
135
272
    return 1;
136
15.1k
}
137
138
139
int
140
_PyFuture_FromAST(mod_ty mod, PyObject *filename, _PyFutureFeatures *ff)
141
17.8k
{
142
17.8k
    ff->ff_features = 0;
143
17.8k
    ff->ff_location = (_Py_SourceLocation){-1, -1, -1, -1};
144
145
17.8k
    if (!future_parse(ff, mod, filename)) {
146
104
        return 0;
147
104
    }
148
17.7k
    return 1;
149
17.8k
}