blob: d24ae416ff5d1ba03938bc39628925e2c276415e [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 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
64 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 /* A subsequent pass will detect future imports that don't
67 appear at the beginning of the file. There's one case,
68 however, that is easier to handle here: A series of imports
69 joined by semi-colons, where the first import is a future
70 statement but some subsequent import has the future form
71 but is preceded by a regular import.
72 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000073
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
76 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (done && s->lineno > prev_line)
79 return 1;
80 prev_line = s->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 /* The tests below will return from this function unless it is
83 still possible to find a future statement. The only things
84 that can precede a future statement are another future
85 statement and a doc string.
86 */
87
88 if (s->kind == ImportFrom_kind) {
Kristján Valur Jónssonc5d47d52012-03-23 12:50:53 +000089 identifier modname = s->v.ImportFrom.module;
Benjamin Petersonab79c712012-03-22 08:56:15 -040090 if (modname &&
91 !PyUnicode_CompareWithASCIIString(modname, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 if (done) {
93 PyErr_SetString(PyExc_SyntaxError,
94 ERR_LATE_FUTURE);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000095 PyErr_SyntaxLocationEx(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 return 0;
97 }
98 if (!future_check_features(ff, s, filename))
99 return 0;
100 ff->ff_lineno = s->lineno;
101 }
102 else
103 done = 1;
104 }
105 else if (s->kind == Expr_kind && !found_docstring) {
106 expr_ty e = s->v.Expr.value;
107 if (e->kind != Str_kind)
108 done = 1;
109 else
110 found_docstring = 1;
111 }
112 else
113 done = 1;
114 }
115 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000116}
117
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000119PyFutureFeatures *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120PyFuture_FromAST(mod_ty mod, const char *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 PyFutureFeatures *ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
125 if (ff == NULL) {
126 PyErr_NoMemory();
127 return NULL;
128 }
129 ff->ff_features = 0;
130 ff->ff_lineno = -1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 if (!future_parse(ff, mod, filename)) {
133 PyObject_Free(ff);
134 return NULL;
135 }
136 return ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000137}