2001-02-27 19:07:02 +00:00
|
|
|
#include "Python.h"
|
2021-03-17 22:50:50 +00:00
|
|
|
#include "pycore_ast.h" // _PyAST_GetDocString()
|
2024-04-02 10:34:49 +00:00
|
|
|
#include "pycore_symtable.h" // _PyFutureFeatures
|
2023-07-03 22:35:46 +00:00
|
|
|
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
|
2001-02-27 19:07:02 +00:00
|
|
|
|
|
|
|
#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
|
|
|
|
|
|
|
|
static int
|
2024-04-02 10:34:49 +00:00
|
|
|
future_check_features(_PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
|
2001-02-27 19:07:02 +00:00
|
|
|
{
|
2010-05-09 15:52:27 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
assert(s->kind == ImportFrom_kind);
|
|
|
|
|
2020-09-16 18:42:00 +00:00
|
|
|
asdl_alias_seq *names = s->v.ImportFrom.names;
|
2010-05-09 15:52:27 +00:00
|
|
|
for (i = 0; i < asdl_seq_LEN(names); i++) {
|
|
|
|
alias_ty name = (alias_ty)asdl_seq_GET(names, i);
|
2016-11-20 07:13:07 +00:00
|
|
|
const char *feature = PyUnicode_AsUTF8(name->name);
|
2010-05-09 15:52:27 +00:00
|
|
|
if (!feature)
|
|
|
|
return 0;
|
|
|
|
if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(feature, FUTURE_DIVISION) == 0) {
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
|
|
|
|
continue;
|
|
|
|
} else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
|
|
|
|
ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
|
2015-05-09 15:44:30 +00:00
|
|
|
} else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
|
2018-01-26 20:24:24 +00:00
|
|
|
continue;
|
2018-01-26 16:20:18 +00:00
|
|
|
} else if (strcmp(feature, FUTURE_ANNOTATIONS) == 0) {
|
2021-04-21 11:41:19 +00:00
|
|
|
ff->ff_features |= CO_FUTURE_ANNOTATIONS;
|
2010-05-09 15:52:27 +00:00
|
|
|
} else if (strcmp(feature, "braces") == 0) {
|
|
|
|
PyErr_SetString(PyExc_SyntaxError,
|
|
|
|
"not a chance");
|
2018-09-24 21:12:49 +00:00
|
|
|
PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
|
2010-05-09 15:52:27 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
PyErr_Format(PyExc_SyntaxError,
|
|
|
|
UNDEFINED_FUTURE_FEATURE, feature);
|
2018-09-24 21:12:49 +00:00
|
|
|
PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
|
2010-05-09 15:52:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2001-02-27 19:07:02 +00:00
|
|
|
}
|
|
|
|
|
2005-11-13 18:41:28 +00:00
|
|
|
static int
|
2024-04-02 10:34:49 +00:00
|
|
|
future_parse(_PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
|
2001-02-28 01:58:08 +00:00
|
|
|
{
|
2022-10-31 13:08:03 +00:00
|
|
|
if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) {
|
2010-05-09 15:52:27 +00:00
|
|
|
return 1;
|
2022-10-31 13:08:03 +00:00
|
|
|
}
|
2010-05-09 15:52:27 +00:00
|
|
|
|
2022-10-31 13:08:03 +00:00
|
|
|
Py_ssize_t n = asdl_seq_LEN(mod->v.Module.body);
|
|
|
|
if (n == 0) {
|
2013-03-16 16:15:47 +00:00
|
|
|
return 1;
|
2022-10-31 13:08:03 +00:00
|
|
|
}
|
2013-03-16 16:15:47 +00:00
|
|
|
|
2022-10-31 13:08:03 +00:00
|
|
|
Py_ssize_t i = 0;
|
|
|
|
if (_PyAST_GetDocString(mod->v.Module.body) != NULL) {
|
2018-05-29 09:04:55 +00:00
|
|
|
i++;
|
2022-10-31 13:08:03 +00:00
|
|
|
}
|
2018-05-29 09:04:55 +00:00
|
|
|
|
2022-10-31 13:08:03 +00:00
|
|
|
for (; i < n; i++) {
|
2010-05-09 15:52:27 +00:00
|
|
|
stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
|
|
|
|
|
2022-10-31 13:08:03 +00:00
|
|
|
/* The only things that can precede a future statement
|
|
|
|
* are another future statement and a doc string.
|
|
|
|
*/
|
2010-05-09 15:52:27 +00:00
|
|
|
|
2024-05-02 13:32:20 +00:00
|
|
|
if (s->kind == ImportFrom_kind && s->v.ImportFrom.level == 0) {
|
2012-03-23 12:50:53 +00:00
|
|
|
identifier modname = s->v.ImportFrom.module;
|
2012-03-22 12:56:15 +00:00
|
|
|
if (modname &&
|
2016-11-16 08:17:58 +00:00
|
|
|
_PyUnicode_EqualToASCIIString(modname, "__future__")) {
|
2022-10-31 13:08:03 +00:00
|
|
|
if (!future_check_features(ff, s, filename)) {
|
2010-05-09 15:52:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-10-31 13:08:03 +00:00
|
|
|
ff->ff_location = SRC_LOCATION_FROM_AST(s);
|
2010-05-09 15:52:27 +00:00
|
|
|
}
|
2013-03-16 16:15:47 +00:00
|
|
|
else {
|
2022-10-31 13:08:03 +00:00
|
|
|
return 1;
|
2013-03-16 16:15:47 +00:00
|
|
|
}
|
2010-05-09 15:52:27 +00:00
|
|
|
}
|
2013-03-16 16:15:47 +00:00
|
|
|
else {
|
2022-10-31 13:08:03 +00:00
|
|
|
return 1;
|
2013-03-16 16:15:47 +00:00
|
|
|
}
|
2010-05-09 15:52:27 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2001-02-27 19:07:02 +00:00
|
|
|
}
|
|
|
|
|
2005-10-20 19:59:25 +00:00
|
|
|
|
2022-11-02 15:13:07 +00:00
|
|
|
int
|
2024-04-02 10:34:49 +00:00
|
|
|
_PyFuture_FromAST(mod_ty mod, PyObject *filename, _PyFutureFeatures *ff)
|
2001-02-27 19:07:02 +00:00
|
|
|
{
|
2010-05-09 15:52:27 +00:00
|
|
|
ff->ff_features = 0;
|
2024-04-02 10:34:49 +00:00
|
|
|
ff->ff_location = (_Py_SourceLocation){-1, -1, -1, -1};
|
2010-05-09 15:52:27 +00:00
|
|
|
|
|
|
|
if (!future_parse(ff, mod, filename)) {
|
2022-11-02 15:13:07 +00:00
|
|
|
return 0;
|
2010-05-09 15:52:27 +00:00
|
|
|
}
|
2022-11-02 15:13:07 +00:00
|
|
|
return 1;
|
2001-02-27 19:07:02 +00:00
|
|
|
}
|