blob: d6b653f31593b8fb5e754d3fb8447a0e9da8dc6a [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 "symtable.h"
8
9#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
Benjamin Petersonc0747cf2008-11-03 20:31:38 +000010#define ERR_LATE_FUTURE \
11"from __future__ imports must occur at the beginning of the file"
Jeremy Hylton4db62b12001-02-27 19:07:02 +000012
13static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +000015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 int i;
17 asdl_seq *names;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 assert(s->kind == ImportFrom_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000021 names = s->v.ImportFrom.names;
22 for (i = 0; i < asdl_seq_LEN(names); i++) {
23 alias_ty name = (alias_ty)asdl_seq_GET(names, i);
24 const char *feature = _PyUnicode_AsString(name->name);
25 if (!feature)
26 return 0;
27 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
28 continue;
29 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
30 continue;
31 } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
32 continue;
33 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
34 continue;
35 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
36 continue;
37 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
38 continue;
39 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
40 continue;
41 } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
42 ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
43 } else if (strcmp(feature, "braces") == 0) {
44 PyErr_SetString(PyExc_SyntaxError,
45 "not a chance");
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000046 PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 return 0;
48 } else {
49 PyErr_Format(PyExc_SyntaxError,
50 UNDEFINED_FUTURE_FEATURE, feature);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000051 PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 return 0;
53 }
54 }
55 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000056}
57
Neal Norwitz6576bd82005-11-13 18:41:28 +000058static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000059future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 int i, found_docstring = 0, done = 0, prev_line = 0;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 static PyObject *future;
64 if (!future) {
65 future = PyUnicode_InternFromString("__future__");
66 if (!future)
67 return 0;
68 }
Jeremy Hylton4db62b12001-02-27 19:07:02 +000069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
71 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 /* A subsequent pass will detect future imports that don't
74 appear at the beginning of the file. There's one case,
75 however, that is easier to handle here: A series of imports
76 joined by semi-colons, where the first import is a future
77 statement but some subsequent import has the future form
78 but is preceded by a regular import.
79 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
83 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (done && s->lineno > prev_line)
86 return 1;
87 prev_line = s->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 /* The tests below will return from this function unless it is
90 still possible to find a future statement. The only things
91 that can precede a future statement are another future
92 statement and a doc string.
93 */
94
95 if (s->kind == ImportFrom_kind) {
96 if (s->v.ImportFrom.module == future) {
97 if (done) {
98 PyErr_SetString(PyExc_SyntaxError,
99 ERR_LATE_FUTURE);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000100 PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 return 0;
102 }
103 if (!future_check_features(ff, s, filename))
104 return 0;
105 ff->ff_lineno = s->lineno;
106 }
107 else
108 done = 1;
109 }
110 else if (s->kind == Expr_kind && !found_docstring) {
111 expr_ty e = s->v.Expr.value;
112 if (e->kind != Str_kind)
113 done = 1;
114 else
115 found_docstring = 1;
116 }
117 else
118 done = 1;
119 }
120 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000121}
122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000124PyFutureFeatures *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125PyFuture_FromAST(mod_ty mod, const char *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 PyFutureFeatures *ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
130 if (ff == NULL) {
131 PyErr_NoMemory();
132 return NULL;
133 }
134 ff->ff_features = 0;
135 ff->ff_lineno = -1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 if (!future_parse(ff, mod, filename)) {
138 PyObject_Free(ff);
139 return NULL;
140 }
141 return ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000142}