| 1 | n/a | #include "Python.h" |
|---|
| 2 | n/a | #include "Python-ast.h" |
|---|
| 3 | n/a | #include "node.h" |
|---|
| 4 | n/a | #include "token.h" |
|---|
| 5 | n/a | #include "graminit.h" |
|---|
| 6 | n/a | #include "code.h" |
|---|
| 7 | n/a | #include "symtable.h" |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined" |
|---|
| 10 | n/a | #define ERR_LATE_FUTURE \ |
|---|
| 11 | n/a | "from __future__ imports must occur at the beginning of the file" |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | static int |
|---|
| 14 | n/a | future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename) |
|---|
| 15 | n/a | { |
|---|
| 16 | n/a | int i; |
|---|
| 17 | n/a | asdl_seq *names; |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | assert(s->kind == ImportFrom_kind); |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | names = s->v.ImportFrom.names; |
|---|
| 22 | n/a | for (i = 0; i < asdl_seq_LEN(names); i++) { |
|---|
| 23 | n/a | alias_ty name = (alias_ty)asdl_seq_GET(names, i); |
|---|
| 24 | n/a | const char *feature = PyUnicode_AsUTF8(name->name); |
|---|
| 25 | n/a | if (!feature) |
|---|
| 26 | n/a | return 0; |
|---|
| 27 | n/a | if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) { |
|---|
| 28 | n/a | continue; |
|---|
| 29 | n/a | } else if (strcmp(feature, FUTURE_GENERATORS) == 0) { |
|---|
| 30 | n/a | continue; |
|---|
| 31 | n/a | } else if (strcmp(feature, FUTURE_DIVISION) == 0) { |
|---|
| 32 | n/a | continue; |
|---|
| 33 | n/a | } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) { |
|---|
| 34 | n/a | continue; |
|---|
| 35 | n/a | } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) { |
|---|
| 36 | n/a | continue; |
|---|
| 37 | n/a | } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) { |
|---|
| 38 | n/a | continue; |
|---|
| 39 | n/a | } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) { |
|---|
| 40 | n/a | continue; |
|---|
| 41 | n/a | } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) { |
|---|
| 42 | n/a | ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL; |
|---|
| 43 | n/a | } else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) { |
|---|
| 44 | n/a | ff->ff_features |= CO_FUTURE_GENERATOR_STOP; |
|---|
| 45 | n/a | } else if (strcmp(feature, "braces") == 0) { |
|---|
| 46 | n/a | PyErr_SetString(PyExc_SyntaxError, |
|---|
| 47 | n/a | "not a chance"); |
|---|
| 48 | n/a | PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset); |
|---|
| 49 | n/a | return 0; |
|---|
| 50 | n/a | } else { |
|---|
| 51 | n/a | PyErr_Format(PyExc_SyntaxError, |
|---|
| 52 | n/a | UNDEFINED_FUTURE_FEATURE, feature); |
|---|
| 53 | n/a | PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset); |
|---|
| 54 | n/a | return 0; |
|---|
| 55 | n/a | } |
|---|
| 56 | n/a | } |
|---|
| 57 | n/a | return 1; |
|---|
| 58 | n/a | } |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | static int |
|---|
| 61 | n/a | future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename) |
|---|
| 62 | n/a | { |
|---|
| 63 | n/a | int i, done = 0, prev_line = 0; |
|---|
| 64 | n/a | stmt_ty first; |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) |
|---|
| 67 | n/a | return 1; |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | if (asdl_seq_LEN(mod->v.Module.body) == 0) |
|---|
| 70 | n/a | return 1; |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | /* A subsequent pass will detect future imports that don't |
|---|
| 73 | n/a | appear at the beginning of the file. There's one case, |
|---|
| 74 | n/a | however, that is easier to handle here: A series of imports |
|---|
| 75 | n/a | joined by semi-colons, where the first import is a future |
|---|
| 76 | n/a | statement but some subsequent import has the future form |
|---|
| 77 | n/a | but is preceded by a regular import. |
|---|
| 78 | n/a | */ |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | i = 0; |
|---|
| 81 | n/a | first = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i); |
|---|
| 82 | n/a | if (first->kind == Expr_kind |
|---|
| 83 | n/a | && (first->v.Expr.value->kind == Str_kind |
|---|
| 84 | n/a | || (first->v.Expr.value->kind == Constant_kind |
|---|
| 85 | n/a | && PyUnicode_CheckExact(first->v.Expr.value->v.Constant.value)))) |
|---|
| 86 | n/a | i++; |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | for (; i < asdl_seq_LEN(mod->v.Module.body); i++) { |
|---|
| 90 | n/a | stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i); |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | if (done && s->lineno > prev_line) |
|---|
| 93 | n/a | return 1; |
|---|
| 94 | n/a | prev_line = s->lineno; |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | /* The tests below will return from this function unless it is |
|---|
| 97 | n/a | still possible to find a future statement. The only things |
|---|
| 98 | n/a | that can precede a future statement are another future |
|---|
| 99 | n/a | statement and a doc string. |
|---|
| 100 | n/a | */ |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | if (s->kind == ImportFrom_kind) { |
|---|
| 103 | n/a | identifier modname = s->v.ImportFrom.module; |
|---|
| 104 | n/a | if (modname && |
|---|
| 105 | n/a | _PyUnicode_EqualToASCIIString(modname, "__future__")) { |
|---|
| 106 | n/a | if (done) { |
|---|
| 107 | n/a | PyErr_SetString(PyExc_SyntaxError, |
|---|
| 108 | n/a | ERR_LATE_FUTURE); |
|---|
| 109 | n/a | PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset); |
|---|
| 110 | n/a | return 0; |
|---|
| 111 | n/a | } |
|---|
| 112 | n/a | if (!future_check_features(ff, s, filename)) |
|---|
| 113 | n/a | return 0; |
|---|
| 114 | n/a | ff->ff_lineno = s->lineno; |
|---|
| 115 | n/a | } |
|---|
| 116 | n/a | else { |
|---|
| 117 | n/a | done = 1; |
|---|
| 118 | n/a | } |
|---|
| 119 | n/a | } |
|---|
| 120 | n/a | else { |
|---|
| 121 | n/a | done = 1; |
|---|
| 122 | n/a | } |
|---|
| 123 | n/a | } |
|---|
| 124 | n/a | return 1; |
|---|
| 125 | n/a | } |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | PyFutureFeatures * |
|---|
| 129 | n/a | PyFuture_FromASTObject(mod_ty mod, PyObject *filename) |
|---|
| 130 | n/a | { |
|---|
| 131 | n/a | PyFutureFeatures *ff; |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures)); |
|---|
| 134 | n/a | if (ff == NULL) { |
|---|
| 135 | n/a | PyErr_NoMemory(); |
|---|
| 136 | n/a | return NULL; |
|---|
| 137 | n/a | } |
|---|
| 138 | n/a | ff->ff_features = 0; |
|---|
| 139 | n/a | ff->ff_lineno = -1; |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | if (!future_parse(ff, mod, filename)) { |
|---|
| 142 | n/a | PyObject_Free(ff); |
|---|
| 143 | n/a | return NULL; |
|---|
| 144 | n/a | } |
|---|
| 145 | n/a | return ff; |
|---|
| 146 | n/a | } |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | PyFutureFeatures * |
|---|
| 150 | n/a | PyFuture_FromAST(mod_ty mod, const char *filename_str) |
|---|
| 151 | n/a | { |
|---|
| 152 | n/a | PyFutureFeatures *ff; |
|---|
| 153 | n/a | PyObject *filename; |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | filename = PyUnicode_DecodeFSDefault(filename_str); |
|---|
| 156 | n/a | if (filename == NULL) |
|---|
| 157 | n/a | return NULL; |
|---|
| 158 | n/a | ff = PyFuture_FromASTObject(mod, filename); |
|---|
| 159 | n/a | Py_DECREF(filename); |
|---|
| 160 | n/a | return ff; |
|---|
| 161 | n/a | } |
|---|