comparison Python/future.c @ 61467:f197407bdba2 3.1

Recorded merge of revisions 81032 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ................ r81032 | antoine.pitrou | 2010-05-09 17:52:27 +0200 (dim., 09 mai 2010) | 9 lines Recorded merge of revisions 81029 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines Untabify C files. Will watch buildbots. ........ ................
author Antoine Pitrou <solipsis@pitrou.net>
date Sun, 09 May 2010 16:14:21 +0000
parents 0530aadff696
children 861c35cef7aa
comparison
equal deleted inserted replaced
61459:f8c19c5f5dd2 61467:f197407bdba2
12 "from __future__ imports must occur at the beginning of the file" 12 "from __future__ imports must occur at the beginning of the file"
13 13
14 static int 14 static int
15 future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename) 15 future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
16 { 16 {
17 int i; 17 int i;
18 asdl_seq *names; 18 asdl_seq *names;
19 19
20 assert(s->kind == ImportFrom_kind); 20 assert(s->kind == ImportFrom_kind);
21 21
22 names = s->v.ImportFrom.names; 22 names = s->v.ImportFrom.names;
23 for (i = 0; i < asdl_seq_LEN(names); i++) { 23 for (i = 0; i < asdl_seq_LEN(names); i++) {
24 alias_ty name = (alias_ty)asdl_seq_GET(names, i); 24 alias_ty name = (alias_ty)asdl_seq_GET(names, i);
25 const char *feature = _PyUnicode_AsString(name->name); 25 const char *feature = _PyUnicode_AsString(name->name);
26 if (!feature) 26 if (!feature)
27 return 0; 27 return 0;
28 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) { 28 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
29 continue; 29 continue;
30 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) { 30 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
31 continue; 31 continue;
32 } else if (strcmp(feature, FUTURE_DIVISION) == 0) { 32 } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
33 continue; 33 continue;
34 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) { 34 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
35 continue; 35 continue;
36 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) { 36 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
37 continue; 37 continue;
38 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) { 38 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
39 continue; 39 continue;
40 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) { 40 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
41 continue; 41 continue;
42 } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) { 42 } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
43 ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL; 43 ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
44 } else if (strcmp(feature, "braces") == 0) { 44 } else if (strcmp(feature, "braces") == 0) {
45 PyErr_SetString(PyExc_SyntaxError, 45 PyErr_SetString(PyExc_SyntaxError,
46 "not a chance"); 46 "not a chance");
47 PyErr_SyntaxLocation(filename, s->lineno); 47 PyErr_SyntaxLocation(filename, s->lineno);
48 return 0; 48 return 0;
49 } else { 49 } else {
50 PyErr_Format(PyExc_SyntaxError, 50 PyErr_Format(PyExc_SyntaxError,
51 UNDEFINED_FUTURE_FEATURE, feature); 51 UNDEFINED_FUTURE_FEATURE, feature);
52 PyErr_SyntaxLocation(filename, s->lineno); 52 PyErr_SyntaxLocation(filename, s->lineno);
53 return 0; 53 return 0;
54 } 54 }
55 } 55 }
56 return 1; 56 return 1;
57 } 57 }
58 58
59 static int 59 static int
60 future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename) 60 future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
61 { 61 {
62 int i, found_docstring = 0, done = 0, prev_line = 0; 62 int i, found_docstring = 0, done = 0, prev_line = 0;
63 63
64 static PyObject *future; 64 static PyObject *future;
65 if (!future) { 65 if (!future) {
66 future = PyUnicode_InternFromString("__future__"); 66 future = PyUnicode_InternFromString("__future__");
67 if (!future) 67 if (!future)
68 return 0; 68 return 0;
69 } 69 }
70 70
71 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) 71 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
72 return 1; 72 return 1;
73 73
74 /* A subsequent pass will detect future imports that don't 74 /* A subsequent pass will detect future imports that don't
75 appear at the beginning of the file. There's one case, 75 appear at the beginning of the file. There's one case,
76 however, that is easier to handle here: A series of imports 76 however, that is easier to handle here: A series of imports
77 joined by semi-colons, where the first import is a future 77 joined by semi-colons, where the first import is a future
78 statement but some subsequent import has the future form 78 statement but some subsequent import has the future form
79 but is preceded by a regular import. 79 but is preceded by a regular import.
80 */ 80 */
81
82 81
83 for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
84 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
85 82
86 if (done && s->lineno > prev_line) 83 for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
87 return 1; 84 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
88 prev_line = s->lineno;
89 85
90 /* The tests below will return from this function unless it is 86 if (done && s->lineno > prev_line)
91 still possible to find a future statement. The only things 87 return 1;
92 that can precede a future statement are another future 88 prev_line = s->lineno;
93 statement and a doc string.
94 */
95 89
96 if (s->kind == ImportFrom_kind) { 90 /* The tests below will return from this function unless it is
97 if (s->v.ImportFrom.module == future) { 91 still possible to find a future statement. The only things
98 if (done) { 92 that can precede a future statement are another future
99 PyErr_SetString(PyExc_SyntaxError, 93 statement and a doc string.
100 ERR_LATE_FUTURE); 94 */
101 PyErr_SyntaxLocation(filename, 95
102 s->lineno); 96 if (s->kind == ImportFrom_kind) {
103 return 0; 97 if (s->v.ImportFrom.module == future) {
104 } 98 if (done) {
105 if (!future_check_features(ff, s, filename)) 99 PyErr_SetString(PyExc_SyntaxError,
106 return 0; 100 ERR_LATE_FUTURE);
107 ff->ff_lineno = s->lineno; 101 PyErr_SyntaxLocation(filename,
108 } 102 s->lineno);
109 else 103 return 0;
110 done = 1; 104 }
111 } 105 if (!future_check_features(ff, s, filename))
112 else if (s->kind == Expr_kind && !found_docstring) { 106 return 0;
113 expr_ty e = s->v.Expr.value; 107 ff->ff_lineno = s->lineno;
114 if (e->kind != Str_kind) 108 }
115 done = 1; 109 else
116 else 110 done = 1;
117 found_docstring = 1; 111 }
118 } 112 else if (s->kind == Expr_kind && !found_docstring) {
119 else 113 expr_ty e = s->v.Expr.value;
120 done = 1; 114 if (e->kind != Str_kind)
121 } 115 done = 1;
122 return 1; 116 else
117 found_docstring = 1;
118 }
119 else
120 done = 1;
121 }
122 return 1;
123 } 123 }
124 124
125 125
126 PyFutureFeatures * 126 PyFutureFeatures *
127 PyFuture_FromAST(mod_ty mod, const char *filename) 127 PyFuture_FromAST(mod_ty mod, const char *filename)
128 { 128 {
129 PyFutureFeatures *ff; 129 PyFutureFeatures *ff;
130 130
131 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures)); 131 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
132 if (ff == NULL) { 132 if (ff == NULL) {
133 PyErr_NoMemory(); 133 PyErr_NoMemory();
134 return NULL; 134 return NULL;
135 } 135 }
136 ff->ff_features = 0; 136 ff->ff_features = 0;
137 ff->ff_lineno = -1; 137 ff->ff_lineno = -1;
138 138
139 if (!future_parse(ff, mod, filename)) { 139 if (!future_parse(ff, mod, filename)) {
140 PyObject_Free(ff); 140 PyObject_Free(ff);
141 return NULL; 141 return NULL;
142 } 142 }
143 return ff; 143 return ff;
144 } 144 }