blob: 9158df45c0d6fe23c3ea972d7c28f291af7e947b [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 "compile.h"
8#include "symtable.h"
9
10#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
11
12static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +000014{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000015 int i;
16 asdl_seq *names;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000017
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000018 assert(s->kind == ImportFrom_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000019
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000020 names = s->v.ImportFrom.names;
21 for (i = 0; i < asdl_seq_LEN(names); i++) {
22 alias_ty name = (alias_ty)asdl_seq_GET(names, i);
23 const char *feature = PyString_AsString(name->name);
24 if (!feature)
25 return 0;
26 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
27 continue;
28 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
29 continue;
30 } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
31 ff->ff_features |= CO_FUTURE_DIVISION;
32 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
33 ff->ff_features |= CO_FUTURE_ABSOLUTE_IMPORT;
34 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
35 ff->ff_features |= CO_FUTURE_WITH_STATEMENT;
36 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
37 ff->ff_features |= CO_FUTURE_PRINT_FUNCTION;
38 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
39 ff->ff_features |= CO_FUTURE_UNICODE_LITERALS;
40 } else if (strcmp(feature, "braces") == 0) {
41 PyErr_SetString(PyExc_SyntaxError,
42 "not a chance");
43 PyErr_SyntaxLocation(filename, s->lineno);
44 return 0;
45 } else {
46 PyErr_Format(PyExc_SyntaxError,
47 UNDEFINED_FUTURE_FEATURE, feature);
48 PyErr_SyntaxLocation(filename, s->lineno);
49 return 0;
50 }
51 }
52 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000053}
54
Neal Norwitz6576bd82005-11-13 18:41:28 +000055static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000057{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000058 int i, found_docstring = 0, done = 0, prev_line = 0;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000059
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000060 static PyObject *future;
61 if (!future) {
62 future = PyString_InternFromString("__future__");
63 if (!future)
64 return 0;
65 }
Jeremy Hylton4db62b12001-02-27 19:07:02 +000066
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000067 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
68 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000069
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000070 /* A subsequent pass will detect future imports that don't
71 appear at the beginning of the file. There's one case,
72 however, that is easier to handle here: A series of imports
73 joined by semi-colons, where the first import is a future
74 statement but some subsequent import has the future form
75 but is preceded by a regular import.
76 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000079 for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
80 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000082 if (done && s->lineno > prev_line)
83 return 1;
84 prev_line = s->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000086 /* 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) {
93 if (s->v.ImportFrom.module == future) {
94 if (done) {
95 PyErr_SetString(PyExc_SyntaxError,
96 ERR_LATE_FUTURE);
97 PyErr_SyntaxLocation(filename,
98 s->lineno);
99 return 0;
100 }
101 if (!future_check_features(ff, s, filename))
102 return 0;
103 ff->ff_lineno = s->lineno;
104 }
105 else
106 done = 1;
107 }
108 else if (s->kind == Expr_kind && !found_docstring) {
109 expr_ty e = s->v.Expr.value;
110 if (e->kind != Str_kind)
111 done = 1;
112 else
113 found_docstring = 1;
114 }
115 else
116 done = 1;
117 }
118 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000119}
120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000122PyFutureFeatures *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123PyFuture_FromAST(mod_ty mod, const char *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000124{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000125 PyFutureFeatures *ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000126
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000127 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
128 if (ff == NULL) {
129 PyErr_NoMemory();
130 return NULL;
131 }
132 ff->ff_features = 0;
133 ff->ff_lineno = -1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000134
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000135 if (!future_parse(ff, mod, filename)) {
136 PyObject_Free(ff);
137 return NULL;
138 }
139 return ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000140}