Added a test for the ability to specify a class attribute in Formatter configuration...
[python.git] / Python / future.c
blobda56dfb14c5fb1322fe19052c16d8b65b4e07b72
1 #include "Python.h"
2 #include "Python-ast.h"
3 #include "node.h"
4 #include "token.h"
5 #include "graminit.h"
6 #include "code.h"
7 #include "compile.h"
8 #include "symtable.h"
10 #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
12 static int
13 future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
15 int i;
16 asdl_seq *names;
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);
24 if (!feature)
25 return 0;
26 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
27 continue;
28 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
29 continue;
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,
34 "not a chance");
35 PyErr_SyntaxLocation(filename, s->lineno);
36 return 0;
37 } else {
38 PyErr_Format(PyExc_SyntaxError,
39 UNDEFINED_FUTURE_FEATURE, feature);
40 PyErr_SyntaxLocation(filename, s->lineno);
41 return 0;
44 return 1;
47 static int
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;
53 if (!future) {
54 future = PyString_InternFromString("__future__");
55 if (!future)
56 return 0;
59 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
60 return 1;
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)
75 return 1;
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) {
86 if (done) {
87 PyErr_SetString(PyExc_SyntaxError,
88 ERR_LATE_FUTURE);
89 PyErr_SyntaxLocation(filename,
90 s->lineno);
91 return 0;
93 if (!future_check_features(ff, s, filename))
94 return 0;
95 ff->ff_lineno = s->lineno;
97 else
98 done = 1;
100 else if (s->kind == Expr_kind && !found_docstring) {
101 expr_ty e = s->v.Expr.value;
102 if (e->kind != Str_kind)
103 done = 1;
104 else
105 found_docstring = 1;
107 else
108 done = 1;
110 return 1;
114 PyFutureFeatures *
115 PyFuture_FromAST(mod_ty mod, const char *filename)
117 PyFutureFeatures *ff;
119 ff = (PyFutureFeatures *)PyMem_Malloc(sizeof(PyFutureFeatures));
120 if (ff == NULL)
121 return NULL;
122 ff->ff_features = 0;
123 ff->ff_lineno = -1;
125 if (!future_parse(ff, mod, filename)) {
126 PyMem_Free((void *)ff);
127 return NULL;
129 return ff;