2 #include "Python-ast.h"
10 #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
13 future_check_features(PyFutureFeatures
*ff
, stmt_ty s
, const char *filename
)
18 assert(s
->kind
== ImportFrom_kind
);
20 names
= s
->v
.ImportFrom
.names
;
21 for (i
= 0; i
< asdl_seq_LEN(names
); i
++) {
22 alias_ty name
= asdl_seq_GET(names
, i
);
23 const char *feature
= PyString_AsString(name
->name
);
26 if (strcmp(feature
, FUTURE_NESTED_SCOPES
) == 0) {
28 } else if (strcmp(feature
, FUTURE_GENERATORS
) == 0) {
30 } else if (strcmp(feature
, FUTURE_DIVISION
) == 0) {
31 ff
->ff_features
|= CO_FUTURE_DIVISION
;
32 } else if (strcmp(feature
, "braces") == 0) {
33 PyErr_SetString(PyExc_SyntaxError
,
35 PyErr_SyntaxLocation(filename
, s
->lineno
);
38 PyErr_Format(PyExc_SyntaxError
,
39 UNDEFINED_FUTURE_FEATURE
, feature
);
40 PyErr_SyntaxLocation(filename
, s
->lineno
);
48 future_parse(PyFutureFeatures
*ff
, mod_ty mod
, const char *filename
)
50 int i
, found_docstring
= 0, done
= 0, prev_line
= 0;
52 static PyObject
*future
;
54 future
= PyString_InternFromString("__future__");
59 if (!(mod
->kind
== Module_kind
|| mod
->kind
== Interactive_kind
))
62 /* A subsequent pass will detect future imports that don't
63 appear at the beginning of the file. There's one case,
64 however, that is easier to handl here: A series of imports
65 joined by semi-colons, where the first import is a future
66 statement but some subsequent import has the future form
67 but is preceded by a regular import.
71 for (i
= 0; i
< asdl_seq_LEN(mod
->v
.Module
.body
); i
++) {
72 stmt_ty s
= asdl_seq_GET(mod
->v
.Module
.body
, i
);
74 if (done
&& s
->lineno
> prev_line
)
76 prev_line
= s
->lineno
;
78 /* The tests below will return from this function unless it is
79 still possible to find a future statement. The only things
80 that can precede a future statement are another future
81 statement and a doc string.
84 if (s
->kind
== ImportFrom_kind
) {
85 if (s
->v
.ImportFrom
.module
== future
) {
87 PyErr_SetString(PyExc_SyntaxError
,
89 PyErr_SyntaxLocation(filename
,
93 if (!future_check_features(ff
, s
, filename
))
95 ff
->ff_lineno
= s
->lineno
;
100 else if (s
->kind
== Expr_kind
&& !found_docstring
) {
101 expr_ty e
= s
->v
.Expr
.value
;
102 if (e
->kind
!= Str_kind
)
115 PyFuture_FromAST(mod_ty mod
, const char *filename
)
117 PyFutureFeatures
*ff
;
119 ff
= (PyFutureFeatures
*)PyMem_Malloc(sizeof(PyFutureFeatures
));
125 if (!future_parse(ff
, mod
, filename
)) {
126 PyMem_Free((void *)ff
);