Coverage Report

Created: 2026-07-14 06:16

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_symtable.h"      // _PyFutureFeatures
4
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
5
6
32
#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
7
8
static int
9
future_check_features(_PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
10
387
{
11
387
    Py_ssize_t i;
12
13
387
    assert(s->kind == ImportFrom_kind);
14
15
387
    asdl_alias_seq *names = s->v.ImportFrom.names;
16
759
    for (i = 0; i < asdl_seq_LEN(names); i++) {
17
405
        alias_ty name = (alias_ty)asdl_seq_GET(names, i);
18
405
        const char *feature = PyUnicode_AsUTF8(name->name);
19
405
        if (!feature)
20
0
            return 0;
21
405
        if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
22
21
            continue;
23
384
        } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
24
10
            continue;
25
374
        } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
26
10
            continue;
27
364
        } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
28
75
            continue;
29
289
        } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
30
18
            continue;
31
271
        } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
32
1
            continue;
33
270
        } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
34
20
            continue;
35
250
        } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
36
75
            ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
37
175
        } else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
38
18
            continue;
39
157
        } else if (strcmp(feature, FUTURE_ANNOTATIONS) == 0) {
40
124
            ff->ff_features |= CO_FUTURE_ANNOTATIONS;
41
124
        } else if (strcmp(feature, "braces") == 0) {
42
1
            PyErr_SetString(PyExc_SyntaxError,
43
1
                            "not a chance");
44
1
            PyErr_RangedSyntaxLocationObject(filename,
45
1
                                             name->lineno,
46
1
                                             name->col_offset + 1,
47
1
                                             name->end_lineno,
48
1
                                             name->end_col_offset + 1);
49
1
            return 0;
50
32
        } else {
51
32
            PyErr_Format(PyExc_SyntaxError,
52
32
                         UNDEFINED_FUTURE_FEATURE, feature);
53
32
            PyErr_RangedSyntaxLocationObject(filename,
54
32
                                             name->lineno,
55
32
                                             name->col_offset + 1,
56
32
                                             name->end_lineno,
57
32
                                             name->end_col_offset + 1);
58
32
            return 0;
59
32
        }
60
405
    }
61
354
    return 1;
62
387
}
63
64
static int
65
future_parse(_PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
66
7.78k
{
67
7.78k
    if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) {
68
223
        return 1;
69
223
    }
70
71
7.56k
    Py_ssize_t n = asdl_seq_LEN(mod->v.Module.body);
72
7.56k
    if (n == 0) {
73
894
        return 1;
74
894
    }
75
76
6.67k
    Py_ssize_t i = 0;
77
6.67k
    if (_PyAST_GetDocString(mod->v.Module.body) != NULL) {
78
442
        i++;
79
442
    }
80
81
7.02k
    for (; i < n; i++) {
82
6.70k
        stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
83
84
        /* The only things that can precede a future statement
85
         *  are another future statement and a doc string.
86
         */
87
88
6.70k
        if (s->kind == ImportFrom_kind && s->v.ImportFrom.level == 0) {
89
600
            identifier modname = s->v.ImportFrom.module;
90
600
            if (modname &&
91
600
                _PyUnicode_EqualToASCIIString(modname, "__future__")) {
92
387
                if (!future_check_features(ff, s, filename)) {
93
33
                    return 0;
94
33
                }
95
354
                ff->ff_location = SRC_LOCATION_FROM_AST(s);
96
354
            }
97
213
            else {
98
213
                return 1;
99
213
            }
100
600
        }
101
6.10k
        else {
102
6.10k
            return 1;
103
6.10k
        }
104
6.70k
    }
105
316
    return 1;
106
6.67k
}
107
108
109
int
110
_PyFuture_FromAST(mod_ty mod, PyObject *filename, _PyFutureFeatures *ff)
111
7.78k
{
112
7.78k
    ff->ff_features = 0;
113
7.78k
    ff->ff_location = (_Py_SourceLocation){-1, -1, -1, -1};
114
115
7.78k
    if (!future_parse(ff, mod, filename)) {
116
33
        return 0;
117
33
    }
118
7.75k
    return 1;
119
7.78k
}