blob: da56dfb14c5fb1322fe19052c16d8b65b4e07b72 [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"
11
12static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +000014{
15 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000016 asdl_seq *names;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000017
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018 assert(s->kind == ImportFrom_kind);
19
20 names = s->v.ImportFrom.names;
21 for (i = 0; i < asdl_seq_LEN(names); i++) {
22 alias_ty name = asdl_seq_GET(names, i);
Neal Norwitzb15ec092005-12-06 07:26:02 +000023 const char *feature = PyString_AsString(name->name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024 if (!feature)
25 return 0;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000026 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
Jeremy Hyltonb857ba22001-08-10 21:41:33 +000027 continue;
Guido van Rossumb09f7ed2001-07-15 21:08:29 +000028 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
Tim Peters2bbdba32002-04-12 01:20:10 +000029 continue;
Guido van Rossum4668b002001-08-08 05:00:18 +000030 } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
Jeremy Hyltonb857ba22001-08-10 21:41:33 +000031 ff->ff_features |= CO_FUTURE_DIVISION;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +000032 } else if (strcmp(feature, "braces") == 0) {
33 PyErr_SetString(PyExc_SyntaxError,
34 "not a chance");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035 PyErr_SyntaxLocation(filename, s->lineno);
36 return 0;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000037 } else {
38 PyErr_Format(PyExc_SyntaxError,
39 UNDEFINED_FUTURE_FEATURE, feature);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040 PyErr_SyntaxLocation(filename, s->lineno);
41 return 0;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000042 }
43 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000044 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000045}
46
Neal Norwitz6576bd82005-11-13 18:41:28 +000047static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000049{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000050 int i, found_docstring = 0, done = 0, prev_line = 0;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052 static PyObject *future;
53 if (!future) {
54 future = PyString_InternFromString("__future__");
55 if (!future)
Jeremy Hylton4db62b12001-02-27 19:07:02 +000056 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057 }
Jeremy Hylton4db62b12001-02-27 19:07:02 +000058
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000059 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
Jeremy Hylton4db62b12001-02-27 19:07:02 +000060 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000062 /* A subsequent pass will detect future imports that don't
63 appear at the beginning of the file. There's one case,
64 however, that is easier to handl here: A series of imports
65 joined by semi-colons, where the first import is a future
66 statement but some subsequent import has the future form
67 but is preceded by a regular import.
68 */
69
70
71 for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
72 stmt_ty s = asdl_seq_GET(mod->v.Module.body, i);
73
74 if (done && s->lineno > prev_line)
75 return 1;
76 prev_line = s->lineno;
77
78 /* The tests below will return from this function unless it is
79 still possible to find a future statement. The only things
80 that can precede a future statement are another future
81 statement and a doc string.
82 */
83
84 if (s->kind == ImportFrom_kind) {
85 if (s->v.ImportFrom.module == future) {
86 if (done) {
87 PyErr_SetString(PyExc_SyntaxError,
88 ERR_LATE_FUTURE);
89 PyErr_SyntaxLocation(filename,
90 s->lineno);
91 return 0;
92 }
93 if (!future_check_features(ff, s, filename))
94 return 0;
95 ff->ff_lineno = s->lineno;
96 }
97 else
98 done = 1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000099 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100 else if (s->kind == Expr_kind && !found_docstring) {
101 expr_ty e = s->v.Expr.value;
102 if (e->kind != Str_kind)
103 done = 1;
104 else
105 found_docstring = 1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +0000106 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107 else
108 done = 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000109 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000111}
112
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000114PyFutureFeatures *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000115PyFuture_FromAST(mod_ty mod, const char *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000116{
117 PyFutureFeatures *ff;
118
119 ff = (PyFutureFeatures *)PyMem_Malloc(sizeof(PyFutureFeatures));
120 if (ff == NULL)
121 return NULL;
Jeremy Hyltonb857ba22001-08-10 21:41:33 +0000122 ff->ff_features = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123 ff->ff_lineno = -1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125 if (!future_parse(ff, mod, filename)) {
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000126 PyMem_Free((void *)ff);
127 return NULL;
128 }
129 return ff;
130}