blob: 1663a38a6fdb3a4c7f32aff732f0d259d7e98d3b [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"
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03008#include "ast.h"
Jeremy Hylton4db62b12001-02-27 19:07:02 +00009
10#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
Benjamin Petersonc0747cf2008-11-03 20:31:38 +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
Victor Stinner14e461d2013-08-26 22:28:21 +020015future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +000016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 int i;
18 asdl_seq *names;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 assert(s->kind == ImportFrom_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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);
Serhiy Storchaka06515832016-11-20 09:13:07 +020025 const char *feature = PyUnicode_AsUTF8(name->name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 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 continue;
34 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
35 continue;
36 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
37 continue;
38 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
39 continue;
40 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
41 continue;
42 } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
43 ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
Yury Selivanov8170e8c2015-05-09 11:44:30 -040044 } else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
Yury Selivanov43c47fe2018-01-26 15:24:24 -050045 continue;
Guido van Rossum95e4d582018-01-26 08:20:18 -080046 } else if (strcmp(feature, FUTURE_ANNOTATIONS) == 0) {
47 ff->ff_features |= CO_FUTURE_ANNOTATIONS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 } else if (strcmp(feature, "braces") == 0) {
49 PyErr_SetString(PyExc_SyntaxError,
50 "not a chance");
Ammar Askar025eb982018-09-24 17:12:49 -040051 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 return 0;
53 } else {
54 PyErr_Format(PyExc_SyntaxError,
55 UNDEFINED_FUTURE_FEATURE, feature);
Ammar Askar025eb982018-09-24 17:12:49 -040056 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 return 0;
58 }
59 }
60 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000061}
62
Neal Norwitz6576bd82005-11-13 18:41:28 +000063static int
Victor Stinner14e461d2013-08-26 22:28:21 +020064future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000065{
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070066 int i, done = 0, prev_line = 0;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
69 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000070
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070071 if (asdl_seq_LEN(mod->v.Module.body) == 0)
72 return 1;
73
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 /* A subsequent pass will detect future imports that don't
75 appear at the beginning of the file. There's one case,
76 however, that is easier to handle here: A series of imports
77 joined by semi-colons, where the first import is a future
78 statement but some subsequent import has the future form
79 but is preceded by a regular import.
80 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +030082 i = 0;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +030083 if (_PyAST_GetDocString(mod->v.Module.body) != NULL)
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +030084 i++;
85
86 for (; i < asdl_seq_LEN(mod->v.Module.body); i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 if (done && s->lineno > prev_line)
90 return 1;
91 prev_line = s->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 /* The tests below will return from this function unless it is
94 still possible to find a future statement. The only things
95 that can precede a future statement are another future
96 statement and a doc string.
97 */
98
99 if (s->kind == ImportFrom_kind) {
Kristján Valur Jónssonc5d47d52012-03-23 12:50:53 +0000100 identifier modname = s->v.ImportFrom.module;
Benjamin Petersonab79c712012-03-22 08:56:15 -0400101 if (modname &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200102 _PyUnicode_EqualToASCIIString(modname, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (done) {
104 PyErr_SetString(PyExc_SyntaxError,
105 ERR_LATE_FUTURE);
Victor Stinner14e461d2013-08-26 22:28:21 +0200106 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 return 0;
108 }
109 if (!future_check_features(ff, s, filename))
110 return 0;
111 ff->ff_lineno = s->lineno;
112 }
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700113 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 done = 1;
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700115 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 }
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700117 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 done = 1;
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 }
121 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000122}
123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000125PyFutureFeatures *
Victor Stinner14e461d2013-08-26 22:28:21 +0200126PyFuture_FromASTObject(mod_ty mod, PyObject *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 PyFutureFeatures *ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
131 if (ff == NULL) {
132 PyErr_NoMemory();
133 return NULL;
134 }
135 ff->ff_features = 0;
136 ff->ff_lineno = -1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if (!future_parse(ff, mod, filename)) {
139 PyObject_Free(ff);
140 return NULL;
141 }
142 return ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000143}
Victor Stinner14e461d2013-08-26 22:28:21 +0200144
145
146PyFutureFeatures *
147PyFuture_FromAST(mod_ty mod, const char *filename_str)
148{
149 PyFutureFeatures *ff;
150 PyObject *filename;
151
152 filename = PyUnicode_DecodeFSDefault(filename_str);
153 if (filename == NULL)
154 return NULL;
155 ff = PyFuture_FromASTObject(mod, filename);
156 Py_DECREF(filename);
157 return ff;
158}