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)
14 future_check_features(PyFutureFeatures
*ff
, node
*n
, char *filename
)
20 REQ(n
, import_stmt
); /* must by from __future__ import ... */
22 for (i
= 3; i
< NCH(n
); i
+= 2) {
24 if (TYPE(ch
) == STAR
) {
25 PyErr_SetString(PyExc_SyntaxError
,
27 PyErr_SyntaxLocation(filename
, ch
->n_lineno
);
30 REQ(ch
, import_as_name
);
31 feature
= STR(CHILD(ch
, 0));
32 if (strcmp(feature
, FUTURE_NESTED_SCOPES
) == 0) {
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
,
41 PyErr_SyntaxLocation(filename
, CHILD(ch
, 0)->n_lineno
);
44 PyErr_Format(PyExc_SyntaxError
,
45 UNDEFINED_FUTURE_FEATURE
, feature
);
46 PyErr_SyntaxLocation(filename
, CHILD(ch
, 0)->n_lineno
);
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
85 future_parse(PyFutureFeatures
*ff
, node
*n
, char *filename
)
93 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
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
))
112 REQ(CHILD(n
, 0), small_stmt
);
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
),
142 /* If we found one and only one, then the
143 current lineno is legal.
146 ff
->ff_last_lineno
= n
->n_lineno
+ 1;
148 ff
->ff_last_lineno
= n
->n_lineno
;
150 if (end_of_future
&& found
)
157 if (TYPE(CHILD(n
, 0)) == simple_stmt
) {
160 } else if (TYPE(CHILD(n
, 0)) == expr_stmt
) {
164 REQ(CHILD(n
, 0), compound_stmt
);
165 ff
->ff_last_lineno
= n
->n_lineno
;
176 if (STR(CHILD(n
, 0))[0] != 'f') { /* from */
177 ff
->ff_last_lineno
= n
->n_lineno
;
181 if (strcmp(STR(CHILD(name
, 0)), "__future__") != 0)
183 if (future_check_features(ff
, n
, filename
) < 0)
185 ff
->ff_last_lineno
= n
->n_lineno
+ 1;
189 /* The cases below -- all of them! -- are necessary to find
190 and skip doc strings. */
212 if (TYPE(CHILD(n
, 0)) == STRING
213 && ff
->ff_found_docstring
== 0) {
214 ff
->ff_found_docstring
= 1;
217 ff
->ff_last_lineno
= n
->n_lineno
;
221 ff
->ff_last_lineno
= n
->n_lineno
;
228 PyNode_Future(node
*n
, char *filename
)
230 PyFutureFeatures
*ff
;
232 ff
= (PyFutureFeatures
*)PyMem_Malloc(sizeof(PyFutureFeatures
));
235 ff
->ff_found_docstring
= 0;
236 ff
->ff_last_lineno
= -1;
239 if (future_parse(ff
, n
, filename
) < 0) {
240 PyMem_Free((void *)ff
);