blob: 75f210769e12540ecdd2bd1ea1bb29f1a3f0d0fe [file] [log] [blame]
Jeremy Hylton4db62b12001-02-27 19:07:02 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
Jeremy Hylton4db62b12001-02-27 19:07:02 +00003#include "node.h"
4#include "token.h"
5#include "graminit.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Jeremy Hylton4db62b12001-02-27 19:07:02 +00007#include "symtable.h"
8
9#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
Benjamin Petersonc0747cf2008-11-03 20:31:38 +000010#define ERR_LATE_FUTURE \
11"from __future__ imports must occur at the beginning of the file"
Jeremy Hylton4db62b12001-02-27 19:07:02 +000012
13static int
Victor Stinner14e461d2013-08-26 22:28:21 +020014future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +000015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 int i;
17 asdl_seq *names;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 assert(s->kind == ImportFrom_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000021 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 Selivanov8170e8c2015-05-09 11:44:30 -040043 } else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
44 ff->ff_features |= CO_FUTURE_GENERATOR_STOP;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 } else if (strcmp(feature, "braces") == 0) {
46 PyErr_SetString(PyExc_SyntaxError,
47 "not a chance");
Victor Stinner14e461d2013-08-26 22:28:21 +020048 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 return 0;
50 } else {
51 PyErr_Format(PyExc_SyntaxError,
52 UNDEFINED_FUTURE_FEATURE, feature);
Victor Stinner14e461d2013-08-26 22:28:21 +020053 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 return 0;
55 }
56 }
57 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000058}
59
Neal Norwitz6576bd82005-11-13 18:41:28 +000060static int
Victor Stinner14e461d2013-08-26 22:28:21 +020061future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000062{
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070063 int i, done = 0, prev_line = 0;
Benjamin Petersonaa14dc32013-03-16 15:38:28 -070064 stmt_ty first;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
67 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000068
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070069 if (asdl_seq_LEN(mod->v.Module.body) == 0)
70 return 1;
71
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 /* 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 Hylton3e0055f2005-10-20 19:59:25 +000079
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070080 i = 0;
Benjamin Petersonaa14dc32013-03-16 15:38:28 -070081 first = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +010082 if (first->kind == Expr_kind
83 && (first->v.Expr.value->kind == Str_kind
84 || (first->v.Expr.value->kind == Constant_kind
85 && PyUnicode_CheckExact(first->v.Expr.value->v.Constant.value))))
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070086 i++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070088
89 for (; i < asdl_seq_LEN(mod->v.Module.body); i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 if (done && s->lineno > prev_line)
93 return 1;
94 prev_line = s->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 /* The tests below will return from this function unless it is
97 still possible to find a future statement. The only things
98 that can precede a future statement are another future
99 statement and a doc string.
100 */
101
102 if (s->kind == ImportFrom_kind) {
Kristján Valur Jónssonc5d47d52012-03-23 12:50:53 +0000103 identifier modname = s->v.ImportFrom.module;
Benjamin Petersonab79c712012-03-22 08:56:15 -0400104 if (modname &&
105 !PyUnicode_CompareWithASCIIString(modname, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 if (done) {
107 PyErr_SetString(PyExc_SyntaxError,
108 ERR_LATE_FUTURE);
Victor Stinner14e461d2013-08-26 22:28:21 +0200109 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 return 0;
111 }
112 if (!future_check_features(ff, s, filename))
113 return 0;
114 ff->ff_lineno = s->lineno;
115 }
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700116 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 done = 1;
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700118 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 }
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700120 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 done = 1;
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700122 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 }
124 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000125}
126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000128PyFutureFeatures *
Victor Stinner14e461d2013-08-26 22:28:21 +0200129PyFuture_FromASTObject(mod_ty mod, PyObject *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 PyFutureFeatures *ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
134 if (ff == NULL) {
135 PyErr_NoMemory();
136 return NULL;
137 }
138 ff->ff_features = 0;
139 ff->ff_lineno = -1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 if (!future_parse(ff, mod, filename)) {
142 PyObject_Free(ff);
143 return NULL;
144 }
145 return ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000146}
Victor Stinner14e461d2013-08-26 22:28:21 +0200147
148
149PyFutureFeatures *
150PyFuture_FromAST(mod_ty mod, const char *filename_str)
151{
152 PyFutureFeatures *ff;
153 PyObject *filename;
154
155 filename = PyUnicode_DecodeFSDefault(filename_str);
156 if (filename == NULL)
157 return NULL;
158 ff = PyFuture_FromASTObject(mod, filename);
159 Py_DECREF(filename);
160 return ff;
161}