Coverage Report

Created: 2025-11-09 06:26

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
9
#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
309
{
11
309
    Py_ssize_t i;
12
13
309
    assert(s->kind == ImportFrom_kind);
14
15
309
    asdl_alias_seq *names = s->v.ImportFrom.names;
16
613
    for (i = 0; i < asdl_seq_LEN(names); i++) {
17
313
        alias_ty name = (alias_ty)asdl_seq_GET(names, i);
18
313
        const char *feature = PyUnicode_AsUTF8(name->name);
19
313
        if (!feature)
20
0
            return 0;
21
313
        if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
22
20
            continue;
23
293
        } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
24
1
            continue;
25
292
        } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
26
1
            continue;
27
291
        } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
28
36
            continue;
29
255
        } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
30
6
            continue;
31
249
        } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
32
1
            continue;
33
248
        } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
34
5
            continue;
35
243
        } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
36
10
            ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
37
233
        } else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
38
83
            continue;
39
150
        } else if (strcmp(feature, FUTURE_ANNOTATIONS) == 0) {
40
141
            ff->ff_features |= CO_FUTURE_ANNOTATIONS;
41
141
        } else if (strcmp(feature, "braces") == 0) {
42
0
            PyErr_SetString(PyExc_SyntaxError,
43
0
                            "not a chance");
44
0
            PyErr_RangedSyntaxLocationObject(filename,
45
0
                                             name->lineno,
46
0
                                             name->col_offset + 1,
47
0
                                             name->end_lineno,
48
0
                                             name->end_col_offset + 1);
49
0
            return 0;
50
9
        } else {
51
9
            PyErr_Format(PyExc_SyntaxError,
52
9
                         UNDEFINED_FUTURE_FEATURE, feature);
53
9
            PyErr_RangedSyntaxLocationObject(filename,
54
9
                                             name->lineno,
55
9
                                             name->col_offset + 1,
56
9
                                             name->end_lineno,
57
9
                                             name->end_col_offset + 1);
58
9
            return 0;
59
9
        }
60
313
    }
61
300
    return 1;
62
309
}
63
64
static int
65
future_parse(_PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
66
6.51k
{
67
6.51k
    if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) {
68
66
        return 1;
69
66
    }
70
71
6.44k
    Py_ssize_t n = asdl_seq_LEN(mod->v.Module.body);
72
6.44k
    if (n == 0) {
73
655
        return 1;
74
655
    }
75
76
5.79k
    Py_ssize_t i = 0;
77
5.79k
    if (_PyAST_GetDocString(mod->v.Module.body) != NULL) {
78
511
        i++;
79
511
    }
80
81
6.09k
    for (; i < n; i++) {
82
5.78k
        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
5.78k
        if (s->kind == ImportFrom_kind && s->v.ImportFrom.level == 0) {
89
337
            identifier modname = s->v.ImportFrom.module;
90
337
            if (modname &&
91
337
                _PyUnicode_EqualToASCIIString(modname, "__future__")) {
92
309
                if (!future_check_features(ff, s, filename)) {
93
9
                    return 0;
94
9
                }
95
300
                ff->ff_location = SRC_LOCATION_FROM_AST(s);
96
300
            }
97
28
            else {
98
28
                return 1;
99
28
            }
100
337
        }
101
5.45k
        else {
102
5.45k
            return 1;
103
5.45k
        }
104
5.78k
    }
105
303
    return 1;
106
5.79k
}
107
108
109
int
110
_PyFuture_FromAST(mod_ty mod, PyObject *filename, _PyFutureFeatures *ff)
111
6.51k
{
112
6.51k
    ff->ff_features = 0;
113
6.51k
    ff->ff_location = (_Py_SourceLocation){-1, -1, -1, -1};
114
115
6.51k
    if (!future_parse(ff, mod, filename)) {
116
9
        return 0;
117
9
    }
118
6.50k
    return 1;
119
6.51k
}