blob: 4b73eb641290528d9c6accd429192b9dce30c116 [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;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 assert(s->kind == ImportFrom_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018
Pablo Galindoa5634c42020-09-16 19:42:00 +010019 asdl_alias_seq *names = s->v.ImportFrom.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 for (i = 0; i < asdl_seq_LEN(names); i++) {
21 alias_ty name = (alias_ty)asdl_seq_GET(names, i);
Serhiy Storchaka06515832016-11-20 09:13:07 +020022 const char *feature = PyUnicode_AsUTF8(name->name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 if (!feature)
24 return 0;
25 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
26 continue;
27 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
28 continue;
29 } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
30 continue;
31 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
32 continue;
33 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
34 continue;
35 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
36 continue;
37 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
38 continue;
39 } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
40 ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
Yury Selivanov8170e8c2015-05-09 11:44:30 -040041 } else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
Yury Selivanov43c47fe2018-01-26 15:24:24 -050042 continue;
Guido van Rossum95e4d582018-01-26 08:20:18 -080043 } else if (strcmp(feature, FUTURE_ANNOTATIONS) == 0) {
Batuhan Taskaya044a1042020-10-06 23:03:02 +030044 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 } else if (strcmp(feature, "braces") == 0) {
46 PyErr_SetString(PyExc_SyntaxError,
47 "not a chance");
Ammar Askar025eb982018-09-24 17:12:49 -040048 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 return 0;
50 } else {
51 PyErr_Format(PyExc_SyntaxError,
52 UNDEFINED_FUTURE_FEATURE, feature);
Ammar Askar025eb982018-09-24 17:12:49 -040053 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 return 0;
55 }
56 }
57 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000058}
59
Neal Norwitz6576bd82005-11-13 18:41:28 +000060static int
Victor Stinner14e461d2013-08-26 22:28:21 +020061future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000062{
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070063 int i, done = 0, prev_line = 0;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
66 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +000067
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070068 if (asdl_seq_LEN(mod->v.Module.body) == 0)
69 return 1;
70
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 /* A subsequent pass will detect future imports that don't
72 appear at the beginning of the file. There's one case,
73 however, that is easier to handle here: A series of imports
74 joined by semi-colons, where the first import is a future
75 statement but some subsequent import has the future form
76 but is preceded by a regular import.
77 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +030079 i = 0;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +030080 if (_PyAST_GetDocString(mod->v.Module.body) != NULL)
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +030081 i++;
82
83 for (; i < asdl_seq_LEN(mod->v.Module.body); i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 if (done && s->lineno > prev_line)
87 return 1;
88 prev_line = s->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 /* The tests below will return from this function unless it is
91 still possible to find a future statement. The only things
92 that can precede a future statement are another future
93 statement and a doc string.
94 */
95
96 if (s->kind == ImportFrom_kind) {
Kristján Valur Jónssonc5d47d52012-03-23 12:50:53 +000097 identifier modname = s->v.ImportFrom.module;
Benjamin Petersonab79c712012-03-22 08:56:15 -040098 if (modname &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +020099 _PyUnicode_EqualToASCIIString(modname, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 if (done) {
101 PyErr_SetString(PyExc_SyntaxError,
102 ERR_LATE_FUTURE);
Victor Stinner14e461d2013-08-26 22:28:21 +0200103 PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 return 0;
105 }
106 if (!future_check_features(ff, s, filename))
107 return 0;
108 ff->ff_lineno = s->lineno;
109 }
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700110 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 done = 1;
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 }
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700114 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 done = 1;
Benjamin Peterson2d6acd22013-03-16 09:15:47 -0700116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 }
118 return 1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000119}
120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000122PyFutureFeatures *
Victor Stinner14e461d2013-08-26 22:28:21 +0200123PyFuture_FromASTObject(mod_ty mod, PyObject *filename)
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 PyFutureFeatures *ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
128 if (ff == NULL) {
129 PyErr_NoMemory();
130 return NULL;
131 }
132 ff->ff_features = 0;
133 ff->ff_lineno = -1;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 if (!future_parse(ff, mod, filename)) {
136 PyObject_Free(ff);
137 return NULL;
138 }
139 return ff;
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000140}
Victor Stinner14e461d2013-08-26 22:28:21 +0200141
142
143PyFutureFeatures *
144PyFuture_FromAST(mod_ty mod, const char *filename_str)
145{
146 PyFutureFeatures *ff;
147 PyObject *filename;
148
149 filename = PyUnicode_DecodeFSDefault(filename_str);
150 if (filename == NULL)
151 return NULL;
152 ff = PyFuture_FromASTObject(mod, filename);
153 Py_DECREF(filename);
154 return ff;
155}