blob: 56da4d8c798b8670661b5fc1e4b4be6aab5187f0 [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 "token.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004#include "code.h"
Jeremy Hylton4db62b12001-02-27 19:07:02 +00005#include "symtable.h"
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03006#include "ast.h"
Jeremy Hylton4db62b12001-02-27 19:07:02 +00007
8#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
Benjamin Petersonc0747cf2008-11-03 20:31:38 +00009#define ERR_LATE_FUTURE \
10"from __future__ imports must occur at the beginning of the file"
Jeremy Hylton4db62b12001-02-27 19:07:02 +000011
12static int
Victor Stinner14e461d2013-08-26 22:28:21 +020013future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +000014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000015 int i;
16 asdl_seq *names;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 assert(s->kind == ImportFrom_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 names = s->v.ImportFrom.names;
21 for (i = 0; i < asdl_seq_LEN(names); i++) {
22 alias_ty name = (alias_ty)asdl_seq_GET(names, i);
Serhiy Storchaka06515832016-11-20 09:13:07 +020023 const char *feature = PyUnicode_AsUTF8(name->name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 if (!feature)
25 return 0;
26 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
27 continue;
28 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
29 continue;
30 } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
31 continue;
32 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
33 continue;
34 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
35 continue;
36 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
37 continue;
38 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
39 continue;
40 } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
41 ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
Yury Selivanov8170e8c2015-05-09 11:44:30 -040042 } else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
Yury Selivanov43c47fe2018-01-26 15:24:24 -050043 continue;
Guido van Rossum95e4d582018-01-26 08:20:18 -080044 } else if (strcmp(feature, FUTURE_ANNOTATIONS) == 0) {
45 ff->ff_features |= CO_FUTURE_ANNOTATIONS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 } else if (strcmp(feature, "braces") == 0) {
47 PyErr_SetString(PyExc_SyntaxError,
48 "not a chance");
Ammar Askar025eb982018-09-24 17:12:49 -040049 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 return 0;
51 } else {
52 PyErr_Format(PyExc_SyntaxError,
53 UNDEFINED_FUTURE_FEATURE, feature);
Ammar Askar025eb982018-09-24 17:12:49 -040054 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 return 0;
56 }
57 }
58 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000059}
60
Neal Norwitz6576bd82005-11-13 18:41:28 +000061static int
Victor Stinner14e461d2013-08-26 22:28:21 +020062future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000063{
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070064 int i, done = 0, prev_line = 0;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
67 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000068
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070069 if (asdl_seq_LEN(mod->v.Module.body) == 0)
70 return 1;
71
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 /* A subsequent pass will detect future imports that don't
73 appear at the beginning of the file. There's one case,
74 however, that is easier to handle here: A series of imports
75 joined by semi-colons, where the first import is a future
76 statement but some subsequent import has the future form
77 but is preceded by a regular import.
78 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000079
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +030080 i = 0;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +030081 if (_PyAST_GetDocString(mod->v.Module.body) != NULL)
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +030082 i++;
83
84 for (; i < asdl_seq_LEN(mod->v.Module.body); i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 if (done && s->lineno > prev_line)
88 return 1;
89 prev_line = s->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 /* The tests below will return from this function unless it is
92 still possible to find a future statement. The only things
93 that can precede a future statement are another future
94 statement and a doc string.
95 */
96
97 if (s->kind == ImportFrom_kind) {
Kristján Valur Jónssonc5d47d52012-03-23 12:50:53 +000098 identifier modname = s->v.ImportFrom.module;
Benjamin Petersonab79c712012-03-22 08:56:15 -040099 if (modname &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200100 _PyUnicode_EqualToASCIIString(modname, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if (done) {
102 PyErr_SetString(PyExc_SyntaxError,
103 ERR_LATE_FUTURE);
Victor Stinner14e461d2013-08-26 22:28:21 +0200104 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 return 0;
106 }
107 if (!future_check_features(ff, s, filename))
108 return 0;
109 ff->ff_lineno = s->lineno;
110 }
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700111 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 done = 1;
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700113 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 }
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700115 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 done = 1;
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700117 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 }
119 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000120}
121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000123PyFutureFeatures *
Victor Stinner14e461d2013-08-26 22:28:21 +0200124PyFuture_FromASTObject(mod_ty mod, PyObject *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 PyFutureFeatures *ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
129 if (ff == NULL) {
130 PyErr_NoMemory();
131 return NULL;
132 }
133 ff->ff_features = 0;
134 ff->ff_lineno = -1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 if (!future_parse(ff, mod, filename)) {
137 PyObject_Free(ff);
138 return NULL;
139 }
140 return ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000141}
Victor Stinner14e461d2013-08-26 22:28:21 +0200142
143
144PyFutureFeatures *
145PyFuture_FromAST(mod_ty mod, const char *filename_str)
146{
147 PyFutureFeatures *ff;
148 PyObject *filename;
149
150 filename = PyUnicode_DecodeFSDefault(filename_str);
151 if (filename == NULL)
152 return NULL;
153 ff = PyFuture_FromASTObject(mod, filename);
154 Py_DECREF(filename);
155 return ff;
156}