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