Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/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
125
#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
651
{
12
651
    Py_ssize_t i;
13
14
651
    assert(s->kind == ImportFrom_kind);
15
16
651
    asdl_alias_seq *names = s->v.ImportFrom.names;
17
1.22k
    for (i = 0; i < asdl_seq_LEN(names); i++) {
18
695
        alias_ty name = (alias_ty)asdl_seq_GET(names, i);
19
695
        const char *feature = PyUnicode_AsUTF8(name->name);
20
695
        if (!feature)
21
0
            return 0;
22
695
        if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
23
68
            continue;
24
627
        } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
25
12
            continue;
26
615
        } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
27
70
            continue;
28
545
        } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
29
67
            continue;
30
478
        } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
31
11
            continue;
32
467
        } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
33
66
            continue;
34
401
        } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
35
36
            continue;
36
365
        } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
37
132
            ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
38
233
        } else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
39
18
            continue;
40
215
        } else if (strcmp(feature, FUTURE_ANNOTATIONS) == 0) {
41
90
            ff->ff_features |= CO_FUTURE_ANNOTATIONS;
42
125
        } else if (strcmp(feature, "braces") == 0) {
43
0
            PyErr_SetString(PyExc_SyntaxError,
44
0
                            "not a chance");
45
0
            PyErr_RangedSyntaxLocationObject(filename,
46
0
                                             name->lineno,
47
0
                                             name->col_offset + 1,
48
0
                                             name->end_lineno,
49
0
                                             name->end_col_offset + 1);
50
0
            return 0;
51
125
        } else {
52
            // Keep this list in sync with the feature checks above.
53
125
            PyObject *future_features = Py_BuildValue("[ssssssssss]",
54
125
                                                      FUTURE_NESTED_SCOPES,
55
125
                                                      FUTURE_GENERATORS,
56
125
                                                      FUTURE_DIVISION,
57
125
                                                      FUTURE_ABSOLUTE_IMPORT,
58
125
                                                      FUTURE_WITH_STATEMENT,
59
125
                                                      FUTURE_PRINT_FUNCTION,
60
125
                                                      FUTURE_UNICODE_LITERALS,
61
125
                                                      FUTURE_BARRY_AS_BDFL,
62
125
                                                      FUTURE_GENERATOR_STOP,
63
125
                                                      FUTURE_ANNOTATIONS);
64
125
            PyObject *suggestion = NULL;
65
125
            if (future_features != NULL) {
66
125
                suggestion = _Py_CalculateSuggestions(future_features,
67
125
                                                      name->name);
68
125
            }
69
125
            if (suggestion != NULL) {
70
40
                PyErr_Format(PyExc_SyntaxError,
71
40
                             UNDEFINED_FUTURE_FEATURE ". Did you mean: %R?",
72
40
                             feature, suggestion);
73
40
                Py_DECREF(suggestion);
74
40
            }
75
85
            else {
76
                // Do not fail on missing suggestion,
77
                // just show the default message.
78
85
                PyErr_Format(PyExc_SyntaxError,
79
85
                             UNDEFINED_FUTURE_FEATURE,
80
85
                             feature);
81
85
            }
82
125
            Py_XDECREF(future_features);
83
125
            PyErr_RangedSyntaxLocationObject(filename,
84
125
                                             name->lineno,
85
125
                                             name->col_offset + 1,
86
125
                                             name->end_lineno,
87
125
                                             name->end_col_offset + 1);
88
125
            return 0;
89
125
        }
90
695
    }
91
526
    return 1;
92
651
}
93
94
static int
95
future_parse(_PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
96
9.04k
{
97
9.04k
    if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) {
98
172
        return 1;
99
172
    }
100
101
8.87k
    Py_ssize_t n = asdl_seq_LEN(mod->v.Module.body);
102
8.87k
    if (n == 0) {
103
1.72k
        return 1;
104
1.72k
    }
105
106
7.15k
    Py_ssize_t i = 0;
107
7.15k
    if (_PyAST_GetDocString(mod->v.Module.body) != NULL) {
108
458
        i++;
109
458
    }
110
111
7.67k
    for (; i < n; i++) {
112
7.32k
        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
7.32k
        if (s->kind == ImportFrom_kind && s->v.ImportFrom.level == 0) {
119
862
            identifier modname = s->v.ImportFrom.module;
120
862
            if (modname &&
121
862
                _PyUnicode_EqualToASCIIString(modname, "__future__")) {
122
651
                if (!future_check_features(ff, s, filename)) {
123
125
                    return 0;
124
125
                }
125
526
                ff->ff_location = SRC_LOCATION_FROM_AST(s);
126
526
            }
127
211
            else {
128
211
                return 1;
129
211
            }
130
862
        }
131
6.46k
        else {
132
6.46k
            return 1;
133
6.46k
        }
134
7.32k
    }
135
351
    return 1;
136
7.15k
}
137
138
139
int
140
_PyFuture_FromAST(mod_ty mod, PyObject *filename, _PyFutureFeatures *ff)
141
9.04k
{
142
9.04k
    ff->ff_features = 0;
143
9.04k
    ff->ff_location = (_Py_SourceLocation){-1, -1, -1, -1};
144
145
9.04k
    if (!future_parse(ff, mod, filename)) {
146
125
        return 0;
147
125
    }
148
8.92k
    return 1;
149
9.04k
}