2 #include "Python-ast.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"
15 future_check_features(PyFutureFeatures
*ff
, stmt_ty s
, const char *filename
)
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
= PyString_AsString(name
->name
);
28 if (strcmp(feature
, FUTURE_NESTED_SCOPES
) == 0) {
30 } else if (strcmp(feature
, FUTURE_GENERATORS
) == 0) {
32 } else if (strcmp(feature
, FUTURE_DIVISION
) == 0) {
33 ff
->ff_features
|= CO_FUTURE_DIVISION
;
34 } else if (strcmp(feature
, FUTURE_ABSOLUTE_IMPORT
) == 0) {
35 ff
->ff_features
|= CO_FUTURE_ABSOLUTE_IMPORT
;
36 } else if (strcmp(feature
, FUTURE_WITH_STATEMENT
) == 0) {
37 ff
->ff_features
|= CO_FUTURE_WITH_STATEMENT
;
38 } else if (strcmp(feature
, FUTURE_PRINT_FUNCTION
) == 0) {
39 ff
->ff_features
|= CO_FUTURE_PRINT_FUNCTION
;
40 } else if (strcmp(feature
, FUTURE_UNICODE_LITERALS
) == 0) {
41 ff
->ff_features
|= CO_FUTURE_UNICODE_LITERALS
;
42 } else if (strcmp(feature
, "braces") == 0) {
43 PyErr_SetString(PyExc_SyntaxError
,
45 PyErr_SyntaxLocation(filename
, s
->lineno
);
48 PyErr_Format(PyExc_SyntaxError
,
49 UNDEFINED_FUTURE_FEATURE
, feature
);
50 PyErr_SyntaxLocation(filename
, s
->lineno
);
58 future_parse(PyFutureFeatures
*ff
, mod_ty mod
, const char *filename
)
60 int i
, found_docstring
= 0, done
= 0, prev_line
= 0;
62 if (!(mod
->kind
== Module_kind
|| mod
->kind
== Interactive_kind
))
65 /* A subsequent pass will detect future imports that don't
66 appear at the beginning of the file. There's one case,
67 however, that is easier to handle here: A series of imports
68 joined by semi-colons, where the first import is a future
69 statement but some subsequent import has the future form
70 but is preceded by a regular import.
74 for (i
= 0; i
< asdl_seq_LEN(mod
->v
.Module
.body
); i
++) {
75 stmt_ty s
= (stmt_ty
)asdl_seq_GET(mod
->v
.Module
.body
, i
);
77 if (done
&& s
->lineno
> prev_line
)
79 prev_line
= s
->lineno
;
81 /* The tests below will return from this function unless it is
82 still possible to find a future statement. The only things
83 that can precede a future statement are another future
84 statement and a doc string.
87 if (s
->kind
== ImportFrom_kind
) {
88 identifier modname
= s
->v
.ImportFrom
.module
;
89 if (modname
&& PyString_GET_SIZE(modname
) == 10 &&
90 !strcmp(PyString_AS_STRING(modname
), "__future__")) {
92 PyErr_SetString(PyExc_SyntaxError
,
94 PyErr_SyntaxLocation(filename
,
98 if (!future_check_features(ff
, s
, filename
))
100 ff
->ff_lineno
= s
->lineno
;
105 else if (s
->kind
== Expr_kind
&& !found_docstring
) {
106 expr_ty e
= s
->v
.Expr
.value
;
107 if (e
->kind
!= Str_kind
)
120 PyFuture_FromAST(mod_ty mod
, const char *filename
)
122 PyFutureFeatures
*ff
;
124 ff
= (PyFutureFeatures
*)PyObject_Malloc(sizeof(PyFutureFeatures
));
132 if (!future_parse(ff
, mod
, filename
)) {