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