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