blob: 551edc6085e4cb710a6217486d035809bc223c24 [file] [log] [blame]
Jeremy Hylton4db62b12001-02-27 19:07:02 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
Jeremy Hylton4db62b12001-02-27 19:07:02 +00003#include "node.h"
4#include "token.h"
5#include "graminit.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Jeremy Hylton4db62b12001-02-27 19:07:02 +00007#include "compile.h"
8#include "symtable.h"
9
10#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
Benjamin Petersonc0747cf2008-11-03 20:31:38 +000011#define ERR_LATE_FUTURE \
12"from __future__ imports must occur at the beginning of the file"
Jeremy Hylton4db62b12001-02-27 19:07:02 +000013
14static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +000016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 int i;
18 asdl_seq *names;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 assert(s->kind == ImportFrom_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 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 = _PyUnicode_AsString(name->name);
26 if (!feature)
27 return 0;
28 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
29 continue;
30 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
31 continue;
32 } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
33 continue;
34 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
35 continue;
36 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
37 continue;
38 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
39 continue;
40 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
41 continue;
42 } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
43 ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
44 } else if (strcmp(feature, "braces") == 0) {
45 PyErr_SetString(PyExc_SyntaxError,
46 "not a chance");
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000047 PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 return 0;
49 } else {
50 PyErr_Format(PyExc_SyntaxError,
51 UNDEFINED_FUTURE_FEATURE, feature);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000052 PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 return 0;
54 }
55 }
56 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000057}
58
Neal Norwitz6576bd82005-11-13 18:41:28 +000059static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000060future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 int i, found_docstring = 0, done = 0, prev_line = 0;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 static PyObject *future;
65 if (!future) {
66 future = PyUnicode_InternFromString("__future__");
67 if (!future)
68 return 0;
69 }
Jeremy Hylton4db62b12001-02-27 19:07:02 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
72 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 /* A subsequent pass will detect future imports that don't
75 appear at the beginning of the file. There's one case,
76 however, that is easier to handle here: A series of imports
77 joined by semi-colons, where the first import is a future
78 statement but some subsequent import has the future form
79 but is preceded by a regular import.
80 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 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);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 if (done && s->lineno > prev_line)
87 return 1;
88 prev_line = s->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 /* The tests below will return from this function unless it is
91 still possible to find a future statement. The only things
92 that can precede a future statement are another future
93 statement and a doc string.
94 */
95
96 if (s->kind == ImportFrom_kind) {
97 if (s->v.ImportFrom.module == future) {
98 if (done) {
99 PyErr_SetString(PyExc_SyntaxError,
100 ERR_LATE_FUTURE);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000101 PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 return 0;
103 }
104 if (!future_check_features(ff, s, filename))
105 return 0;
106 ff->ff_lineno = s->lineno;
107 }
108 else
109 done = 1;
110 }
111 else if (s->kind == Expr_kind && !found_docstring) {
112 expr_ty e = s->v.Expr.value;
113 if (e->kind != Str_kind)
114 done = 1;
115 else
116 found_docstring = 1;
117 }
118 else
119 done = 1;
120 }
121 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000122}
123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000125PyFutureFeatures *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000126PyFuture_FromAST(mod_ty mod, const char *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 PyFutureFeatures *ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
131 if (ff == NULL) {
132 PyErr_NoMemory();
133 return NULL;
134 }
135 ff->ff_features = 0;
136 ff->ff_lineno = -1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if (!future_parse(ff, mod, filename)) {
139 PyObject_Free(ff);
140 return NULL;
141 }
142 return ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000143}