blob: 0e6884598122fe9191baa41286286d52166baf8a [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 Peterson44a90c92008-10-30 23:59:18 +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 Pitrouc83ea132010-05-09 14:46:46 +000017 int i;
18 asdl_seq *names;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000019
Antoine Pitrouc83ea132010-05-09 14:46:46 +000020 assert(s->kind == ImportFrom_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 = PyString_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 ff->ff_features |= CO_FUTURE_DIVISION;
34 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
35 ff->ff_features |= CO_FUTURE_ABSOLUTE_IMPORT;
36 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
37 ff->ff_features |= CO_FUTURE_WITH_STATEMENT;
38 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
39 ff->ff_features |= CO_FUTURE_PRINT_FUNCTION;
40 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
41 ff->ff_features |= CO_FUTURE_UNICODE_LITERALS;
42 } else if (strcmp(feature, "braces") == 0) {
43 PyErr_SetString(PyExc_SyntaxError,
44 "not a chance");
45 PyErr_SyntaxLocation(filename, s->lineno);
46 return 0;
47 } else {
48 PyErr_Format(PyExc_SyntaxError,
49 UNDEFINED_FUTURE_FEATURE, feature);
50 PyErr_SyntaxLocation(filename, s->lineno);
51 return 0;
52 }
53 }
54 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000055}
56
Neal Norwitz6576bd82005-11-13 18:41:28 +000057static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000058future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000059{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000060 int i, found_docstring = 0, done = 0, prev_line = 0;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000061
Antoine Pitrouc83ea132010-05-09 14:46:46 +000062 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
63 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000064
Antoine Pitrouc83ea132010-05-09 14:46:46 +000065 /* A subsequent pass will detect future imports that don't
66 appear at the beginning of the file. There's one case,
67 however, that is easier to handle here: A series of imports
68 joined by semi-colons, where the first import is a future
69 statement but some subsequent import has the future form
70 but is preceded by a regular import.
71 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000072
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000073
Antoine Pitrouc83ea132010-05-09 14:46:46 +000074 for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
75 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000076
Antoine Pitrouc83ea132010-05-09 14:46:46 +000077 if (done && s->lineno > prev_line)
78 return 1;
79 prev_line = s->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080
Antoine Pitrouc83ea132010-05-09 14:46:46 +000081 /* The tests below will return from this function unless it is
82 still possible to find a future statement. The only things
83 that can precede a future statement are another future
84 statement and a doc string.
85 */
86
87 if (s->kind == ImportFrom_kind) {
Benjamin Peterson219a0502012-03-22 10:39:16 -040088 identifier modname = s->v.ImportFrom.module;
Benjamin Petersone90cdaa2012-03-22 08:56:15 -040089 if (modname && PyString_GET_SIZE(modname) == 10 &&
Benjamin Petersoneff19a12012-03-22 08:19:04 -040090 !strcmp(PyString_AS_STRING(modname), "__future__")) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000091 if (done) {
92 PyErr_SetString(PyExc_SyntaxError,
93 ERR_LATE_FUTURE);
94 PyErr_SyntaxLocation(filename,
95 s->lineno);
96 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 Pitrouc83ea132010-05-09 14:46:46 +0000122 PyFutureFeatures *ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000123
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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}