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