Merged revisions 78965 via svnmerge from
[python/dscho.git] / Python / future.c
blob4178541585bf022b09994fc74b2025ee3f82901d
1 #include "Python.h"
2 #include "Python-ast.h"
3 #include "node.h"
4 #include "token.h"
5 #include "graminit.h"
6 #include "code.h"
7 #include "compile.h"
8 #include "symtable.h"
10 #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
11 #define ERR_LATE_FUTURE \
12 "from __future__ imports must occur at the beginning of the file"
14 static int
15 future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
17 int i;
18 asdl_seq *names;
20 assert(s->kind == ImportFrom_kind);
22 names = s->v.ImportFrom.names;
23 for (i = 0; i < asdl_seq_LEN(names); i++) {
24 alias_ty name = (alias_ty)asdl_seq_GET(names, i);
25 const char *feature = _PyUnicode_AsString(name->name);
26 if (!feature)
27 return 0;
28 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
29 continue;
30 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
31 continue;
32 } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
33 continue;
34 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
35 continue;
36 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
37 continue;
38 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
39 continue;
40 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
41 continue;
42 } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
43 ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
44 } else if (strcmp(feature, "braces") == 0) {
45 PyErr_SetString(PyExc_SyntaxError,
46 "not a chance");
47 PyErr_SyntaxLocation(filename, s->lineno);
48 return 0;
49 } else {
50 PyErr_Format(PyExc_SyntaxError,
51 UNDEFINED_FUTURE_FEATURE, feature);
52 PyErr_SyntaxLocation(filename, s->lineno);
53 return 0;
56 return 1;
59 static int
60 future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
62 int i, found_docstring = 0, done = 0, prev_line = 0;
64 static PyObject *future;
65 if (!future) {
66 future = PyUnicode_InternFromString("__future__");
67 if (!future)
68 return 0;
71 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
72 return 1;
74 /* A subsequent pass will detect future imports that don't
75 appear at the beginning of the file. There's one case,
76 however, that is easier to handle here: A series of imports
77 joined by semi-colons, where the first import is a future
78 statement but some subsequent import has the future form
79 but is preceded by a regular import.
83 for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
84 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
86 if (done && s->lineno > prev_line)
87 return 1;
88 prev_line = s->lineno;
90 /* The tests below will return from this function unless it is
91 still possible to find a future statement. The only things
92 that can precede a future statement are another future
93 statement and a doc string.
96 if (s->kind == ImportFrom_kind) {
97 if (s->v.ImportFrom.module == future) {
98 if (done) {
99 PyErr_SetString(PyExc_SyntaxError,
100 ERR_LATE_FUTURE);
101 PyErr_SyntaxLocation(filename,
102 s->lineno);
103 return 0;
105 if (!future_check_features(ff, s, filename))
106 return 0;
107 ff->ff_lineno = s->lineno;
109 else
110 done = 1;
112 else if (s->kind == Expr_kind && !found_docstring) {
113 expr_ty e = s->v.Expr.value;
114 if (e->kind != Str_kind)
115 done = 1;
116 else
117 found_docstring = 1;
119 else
120 done = 1;
122 return 1;
126 PyFutureFeatures *
127 PyFuture_FromAST(mod_ty mod, const char *filename)
129 PyFutureFeatures *ff;
131 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
132 if (ff == NULL) {
133 PyErr_NoMemory();
134 return NULL;
136 ff->ff_features = 0;
137 ff->ff_lineno = -1;
139 if (!future_parse(ff, mod, filename)) {
140 PyObject_Free(ff);
141 return NULL;
143 return ff;