Merged release21-maint changes.
[python/dscho.git] / Python / future.c
blob6b1c0c55b892cba9ddd751ab260786f045caccdd
1 #include "Python.h"
2 #include "node.h"
3 #include "token.h"
4 #include "graminit.h"
5 #include "compile.h"
6 #include "symtable.h"
8 #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
9 #define FUTURE_IMPORT_STAR "future statement does not support import *"
11 #define FUTURE_POSSIBLE(FF) ((FF)->ff_last_lineno == -1)
13 static int
14 future_check_features(PyFutureFeatures *ff, node *n, char *filename)
16 int i;
17 char *feature;
18 node *ch;
20 REQ(n, import_stmt); /* must by from __future__ import ... */
22 for (i = 3; i < NCH(n); i += 2) {
23 ch = CHILD(n, i);
24 if (TYPE(ch) == STAR) {
25 PyErr_SetString(PyExc_SyntaxError,
26 FUTURE_IMPORT_STAR);
27 PyErr_SyntaxLocation(filename, ch->n_lineno);
28 return -1;
30 REQ(ch, import_as_name);
31 feature = STR(CHILD(ch, 0));
32 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
33 continue;
34 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
35 ff->ff_features |= CO_GENERATOR_ALLOWED;
36 } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
37 ff->ff_features |= CO_FUTURE_DIVISION;
38 } else if (strcmp(feature, "braces") == 0) {
39 PyErr_SetString(PyExc_SyntaxError,
40 "not a chance");
41 PyErr_SyntaxLocation(filename, CHILD(ch, 0)->n_lineno);
42 return -1;
43 } else {
44 PyErr_Format(PyExc_SyntaxError,
45 UNDEFINED_FUTURE_FEATURE, feature);
46 PyErr_SyntaxLocation(filename, CHILD(ch, 0)->n_lineno);
47 return -1;
50 return 0;
53 static void
54 future_error(node *n, char *filename)
56 PyErr_SetString(PyExc_SyntaxError,
57 "from __future__ imports must occur at the "
58 "beginning of the file");
59 PyErr_SyntaxLocation(filename, n->n_lineno);
60 /* XXX set filename and lineno */
63 /* Relevant portions of the grammar:
65 single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
66 file_input: (NEWLINE | stmt)* ENDMARKER
67 stmt: simple_stmt | compound_stmt
68 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
69 small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
70 | import_stmt | global_stmt | exec_stmt | assert_stmt
71 import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
72 | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*)
73 import_as_name: NAME [NAME NAME]
74 dotted_as_name: dotted_name [NAME NAME]
75 dotted_name: NAME ('.' NAME)*
78 /* future_parse() return values:
79 -1 indicates an error occurred, e.g. unknown feature name
80 0 indicates no feature was found
81 1 indicates a feature was found
84 static int
85 future_parse(PyFutureFeatures *ff, node *n, char *filename)
87 int i, r;
88 loop:
90 switch (TYPE(n)) {
92 case single_input:
93 if (TYPE(CHILD(n, 0)) == simple_stmt) {
94 n = CHILD(n, 0);
95 goto loop;
97 return 0;
99 case file_input:
100 for (i = 0; i < NCH(n); i++) {
101 node *ch = CHILD(n, i);
102 if (TYPE(ch) == stmt) {
103 r = future_parse(ff, ch, filename);
104 if (!FUTURE_POSSIBLE(ff))
105 return r;
108 return 0;
110 case simple_stmt:
111 if (NCH(n) == 2) {
112 REQ(CHILD(n, 0), small_stmt);
113 n = CHILD(n, 0);
114 goto loop;
115 } else {
116 /* Deal with the special case of a series of
117 small statements on a single line. If a
118 future statement follows some other
119 statement, the SyntaxError is raised here.
120 In all other cases, the symtable pass
121 raises the exception.
123 int found = 0, end_of_future = 0;
125 for (i = 0; i < NCH(n); i += 2) {
126 if (TYPE(CHILD(n, i)) == small_stmt) {
127 r = future_parse(ff, CHILD(n, i),
128 filename);
129 if (r < 1)
130 end_of_future = 1;
131 else {
132 found = 1;
133 if (end_of_future) {
134 future_error(n,
135 filename);
136 return -1;
142 /* If we found one and only one, then the
143 current lineno is legal.
145 if (found)
146 ff->ff_last_lineno = n->n_lineno + 1;
147 else
148 ff->ff_last_lineno = n->n_lineno;
150 if (end_of_future && found)
151 return 1;
152 else
153 return 0;
156 case stmt:
157 if (TYPE(CHILD(n, 0)) == simple_stmt) {
158 n = CHILD(n, 0);
159 goto loop;
160 } else if (TYPE(CHILD(n, 0)) == expr_stmt) {
161 n = CHILD(n, 0);
162 goto loop;
163 } else {
164 REQ(CHILD(n, 0), compound_stmt);
165 ff->ff_last_lineno = n->n_lineno;
166 return 0;
169 case small_stmt:
170 n = CHILD(n, 0);
171 goto loop;
173 case import_stmt: {
174 node *name;
176 if (STR(CHILD(n, 0))[0] != 'f') { /* from */
177 ff->ff_last_lineno = n->n_lineno;
178 return 0;
180 name = CHILD(n, 1);
181 if (strcmp(STR(CHILD(name, 0)), "__future__") != 0)
182 return 0;
183 if (future_check_features(ff, n, filename) < 0)
184 return -1;
185 ff->ff_last_lineno = n->n_lineno + 1;
186 return 1;
189 /* The cases below -- all of them! -- are necessary to find
190 and skip doc strings. */
191 case expr_stmt:
192 case testlist:
193 case test:
194 case and_test:
195 case not_test:
196 case comparison:
197 case expr:
198 case xor_expr:
199 case and_expr:
200 case shift_expr:
201 case arith_expr:
202 case term:
203 case factor:
204 case power:
205 if (NCH(n) == 1) {
206 n = CHILD(n, 0);
207 goto loop;
209 break;
211 case atom:
212 if (TYPE(CHILD(n, 0)) == STRING
213 && ff->ff_found_docstring == 0) {
214 ff->ff_found_docstring = 1;
215 return 0;
217 ff->ff_last_lineno = n->n_lineno;
218 return 0;
220 default:
221 ff->ff_last_lineno = n->n_lineno;
222 return 0;
224 return 0;
227 PyFutureFeatures *
228 PyNode_Future(node *n, char *filename)
230 PyFutureFeatures *ff;
232 ff = (PyFutureFeatures *)PyMem_Malloc(sizeof(PyFutureFeatures));
233 if (ff == NULL)
234 return NULL;
235 ff->ff_found_docstring = 0;
236 ff->ff_last_lineno = -1;
237 ff->ff_features = 0;
239 if (future_parse(ff, n, filename) < 0) {
240 PyMem_Free((void *)ff);
241 return NULL;
243 return ff;