diff options
| author | Guido van Rossum <guido@python.org> | 2019-02-20 18:08:23 -0800 |
|---|---|---|
| committer | Guido van Rossum <guido@python.org> | 2019-03-05 11:36:11 -0800 |
| commit | 0e2403f2f95a21912972422564de37cbe129c195 (patch) | |
| tree | 82d360e7f4c9ea6e2d385c86bf11154de713b717 | |
| parent | d8b3a98c9098c66a714fd5593e1928af0ffbc631 (diff) | |
| download | cpython-git-0e2403f2f95a21912972422564de37cbe129c195.tar.gz | |
Work towards feature_version support
Visible behavior:
- Issue error for `X @ Y` (matrix multiply) if c_feature_version < 5
- Add optional feature_version kw arg to ast.parse() (default -1 which
implies PY_MINOR_VERSION)
- Add feature_version: int = -1 to compile() (via Argument Clinic);
this sets cf_feature_version to the given value if >= 0, else
defaults to PY_MINOR_VERSION
Implementation:
- Add PyAST_obj2mod_ex(): like PyAST_obj2mod() but with feature_version arg;
the latter calls the former with PY_MINOR_VERSION
- Add cf_feature_version to PyCompilerFlags structure;
initialized to PY_MINOR_VERSION everywhere
- Add c_feature_version to struct compiling; initialize from
cf_feature_version
- Add 'c' argument to get_operator()
- In builtin eval() and exec(), default to PY_MINOR_VERSION
TODO:
- Put version-dependent ASYNC/AWAIT keyword scanning back
- Reject async functions, await expressions, and async for/with in minor versions < 5
- Reject async comprehensions in minor versions < 6
- Reject underscores in numeric literals in minor versions < 6
- Reject variable annotations in minor versions < 6
- Reject `X @= Y` in minor versions < 5
| -rw-r--r-- | Include/Python-ast.h | 1 | ||||
| -rw-r--r-- | Include/compile.h | 1 | ||||
| -rw-r--r-- | Lib/ast.py | 9 | ||||
| -rw-r--r-- | Modules/main.c | 2 | ||||
| -rw-r--r-- | Modules/parsermodule.c | 5 | ||||
| -rw-r--r-- | Parser/asdl_c.py | 6 | ||||
| -rw-r--r-- | Python/Python-ast.c | 5 | ||||
| -rw-r--r-- | Python/ast.c | 14 | ||||
| -rw-r--r-- | Python/bltinmodule.c | 11 | ||||
| -rw-r--r-- | Python/clinic/bltinmodule.c.h | 15 | ||||
| -rw-r--r-- | Python/compile.c | 1 | ||||
| -rw-r--r-- | Python/pythonrun.c | 4 |
12 files changed, 58 insertions, 16 deletions
diff --git a/Include/Python-ast.h b/Include/Python-ast.h index 52ba12755e..8265bed8d6 100644 --- a/Include/Python-ast.h +++ b/Include/Python-ast.h @@ -703,6 +703,7 @@ type_ignore_ty _Py_TypeIgnore(int lineno, PyArena *arena); PyObject* PyAST_mod2obj(mod_ty t); mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode); +mod_ty PyAST_obj2mod_ex(PyObject* ast, PyArena* arena, int mode, int feature_version); int PyAST_Check(PyObject* obj); #ifdef __cplusplus diff --git a/Include/compile.h b/Include/compile.h index d0bbed8f55..1370867867 100644 --- a/Include/compile.h +++ b/Include/compile.h @@ -27,6 +27,7 @@ PyAPI_FUNC(PyCodeObject *) PyNode_Compile(struct _node *, const char *); #ifndef Py_LIMITED_API typedef struct { int cf_flags; /* bitmask of CO_xxx flags relevant to future */ + int cf_feature_version; /* minor Python version (PyCF_ONLY_AST) */ } PyCompilerFlags; #endif diff --git a/Lib/ast.py b/Lib/ast.py index 470a74b3b5..819be1f4c3 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -27,7 +27,8 @@ from _ast import * -def parse(source, filename='<unknown>', mode='exec', *, type_comments=False): +def parse(source, filename='<unknown>', mode='exec', *, + type_comments=False, feature_version=-1): """ Parse the source into an AST node. Equivalent to compile(source, filename, mode, PyCF_ONLY_AST). @@ -36,7 +37,11 @@ def parse(source, filename='<unknown>', mode='exec', *, type_comments=False): flags = PyCF_ONLY_AST if type_comments: flags |= PyCF_TYPE_COMMENTS - return compile(source, filename, mode, flags) + return compile(source, filename, mode, flags, + dont_inherit=False, + optimize=-1, + feature_version=feature_version, + ) def literal_eval(node_or_string): diff --git a/Modules/main.c b/Modules/main.c index 34032adca5..e30a8bd8d8 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -821,7 +821,7 @@ pymain_run_python(PyInterpreterState *interp, int *exitcode) Py_DECREF(path0); } - PyCompilerFlags cf = {.cf_flags = 0}; + PyCompilerFlags cf = {.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION}; pymain_header(config); pymain_import_readline(config); diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c index 87f58d340c..d4e2be643e 100644 --- a/Modules/parsermodule.c +++ b/Modules/parsermodule.c @@ -341,6 +341,7 @@ parser_newstobject(node *st, int type) o->st_node = st; o->st_type = type; o->st_flags.cf_flags = 0; + o->st_flags.cf_feature_version = PY_MINOR_VERSION; } else { PyNode_Free(st); @@ -584,8 +585,10 @@ parser_do_parse(PyObject *args, PyObject *kw, const char *argspec, int type) if (n) { res = parser_newstobject(n, type); - if (res) + if (res) { ((PyST_Object *)res)->st_flags.cf_flags = flags & PyCF_MASK; + ((PyST_Object *)res)->st_flags.cf_feature_version = PY_MINOR_VERSION; + } } else { PyParser_SetError(&err); diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 1526995e3f..52247559d1 100644 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -1189,6 +1189,11 @@ PyObject* PyAST_mod2obj(mod_ty t) /* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */ mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode) { + return PyAST_obj2mod_ex(ast, arena, mode, PY_MINOR_VERSION); +} + +mod_ty PyAST_obj2mod_ex(PyObject* ast, PyArena* arena, int mode, int feature_version) +{ mod_ty res; PyObject *req_type[3]; char *req_name[] = {"Module", "Expression", "Interactive"}; @@ -1269,6 +1274,7 @@ def main(srcfile, dump_module=False): f.write("\n") f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") + f.write("mod_ty PyAST_obj2mod_ex(PyObject* ast, PyArena* arena, int mode, int feature_version);\n") f.write("int PyAST_Check(PyObject* obj);\n") f.write('\n') f.write('#ifdef __cplusplus\n') diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 92ec157571..0411f9f07f 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -8900,6 +8900,11 @@ PyObject* PyAST_mod2obj(mod_ty t) /* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */ mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode) { + return PyAST_obj2mod_ex(ast, arena, mode, PY_MINOR_VERSION); +} + +mod_ty PyAST_obj2mod_ex(PyObject* ast, PyArena* arena, int mode, int feature_version) +{ mod_ty res; PyObject *req_type[3]; char *req_name[] = {"Module", "Expression", "Interactive"}; diff --git a/Python/ast.c b/Python/ast.c index 2e960485f4..276e81e558 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -564,6 +564,7 @@ struct compiling { PyArena *c_arena; /* Arena for allocating memory. */ PyObject *c_filename; /* filename */ PyObject *c_normalize; /* Normalization function from unicodedata. */ + int c_feature_version; /* Latest minor version of Python for allowed features */ }; static asdl_seq *seq_for_testlist(struct compiling *, const node *); @@ -783,6 +784,7 @@ PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, /* borrowed reference */ c.c_filename = filename; c.c_normalize = NULL; + c.c_feature_version = flags->cf_feature_version; if (TYPE(n) == encoding_decl) n = CHILD(n, 0); @@ -955,7 +957,7 @@ PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, */ static operator_ty -get_operator(const node *n) +get_operator(struct compiling *c, const node *n) { switch (TYPE(n)) { case VBAR: @@ -975,6 +977,11 @@ get_operator(const node *n) case STAR: return Mult; case AT: + if (c->c_feature_version < 5) { + ast_error(c, n, + "The '@' operator is only supported in Python 3.5 and greater"); + return (operator_ty)0; + } return MatMult; case SLASH: return Div; @@ -2524,7 +2531,7 @@ ast_for_binop(struct compiling *c, const node *n) if (!expr2) return NULL; - newoperator = get_operator(CHILD(n, 1)); + newoperator = get_operator(c, CHILD(n, 1)); if (!newoperator) return NULL; @@ -2539,7 +2546,7 @@ ast_for_binop(struct compiling *c, const node *n) expr_ty tmp_result, tmp; const node* next_oper = CHILD(n, i * 2 + 1); - newoperator = get_operator(next_oper); + newoperator = get_operator(c, next_oper); if (!newoperator) return NULL; @@ -4768,6 +4775,7 @@ fstring_compile_expr(const char *expr_start, const char *expr_end, str[len+2] = 0; cf.cf_flags = PyCF_ONLY_AST; + cf.cf_feature_version = PY_MINOR_VERSION; mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", Py_eval_input, 0); if (!mod_n) { diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index a19b8b8ddc..7a2b259cbd 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -745,6 +745,7 @@ compile as builtin_compile flags: int = 0 dont_inherit: bool(accept={int}) = False optimize: int = -1 + feature_version: int = -1 Compile source into a code object that can be executed by exec() or eval(). @@ -763,8 +764,8 @@ in addition to any features explicitly specified. static PyObject * builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename, const char *mode, int flags, int dont_inherit, - int optimize) -/*[clinic end generated code: output=1fa176e33452bb63 input=0ff726f595eb9fcd]*/ + int optimize, int feature_version) +/*[clinic end generated code: output=b0c09c84f116d3d7 input=5fcc30651a6acaa9]*/ { PyObject *source_copy; const char *str; @@ -775,6 +776,10 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename, PyObject *result; cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8; + cf.cf_feature_version = PY_MINOR_VERSION; + if (feature_version >= 0 && (flags & PyCF_ONLY_AST)) { + cf.cf_feature_version = feature_version; + } if (flags & ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST | PyCF_TYPE_COMMENTS)) @@ -981,6 +986,7 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals, } cf.cf_flags = PyCF_SOURCE_IS_UTF8; + cf.cf_feature_version = PY_MINOR_VERSION; str = source_as_string(source, "eval", "string, bytes or code", &cf, &source_copy); if (str == NULL) return NULL; @@ -1068,6 +1074,7 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals, const char *str; PyCompilerFlags cf; cf.cf_flags = PyCF_SOURCE_IS_UTF8; + cf.cf_feature_version = PY_MINOR_VERSION; str = source_as_string(source, "exec", "string, bytes or code", &cf, &source_copy); diff --git a/Python/clinic/bltinmodule.c.h b/Python/clinic/bltinmodule.c.h index 1b82f773ed..df9970942b 100644 --- a/Python/clinic/bltinmodule.c.h +++ b/Python/clinic/bltinmodule.c.h @@ -151,7 +151,7 @@ exit: PyDoc_STRVAR(builtin_compile__doc__, "compile($module, /, source, filename, mode, flags=0,\n" -" dont_inherit=False, optimize=-1)\n" +" dont_inherit=False, optimize=-1, feature_version=-1)\n" "--\n" "\n" "Compile source into a code object that can be executed by exec() or eval().\n" @@ -173,26 +173,27 @@ PyDoc_STRVAR(builtin_compile__doc__, static PyObject * builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename, const char *mode, int flags, int dont_inherit, - int optimize); + int optimize, int feature_version); static PyObject * builtin_compile(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) { PyObject *return_value = NULL; - static const char * const _keywords[] = {"source", "filename", "mode", "flags", "dont_inherit", "optimize", NULL}; - static _PyArg_Parser _parser = {"OO&s|iii:compile", _keywords, 0}; + static const char * const _keywords[] = {"source", "filename", "mode", "flags", "dont_inherit", "optimize", "feature_version", NULL}; + static _PyArg_Parser _parser = {"OO&s|iiii:compile", _keywords, 0}; PyObject *source; PyObject *filename; const char *mode; int flags = 0; int dont_inherit = 0; int optimize = -1; + int feature_version = -1; if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, - &source, PyUnicode_FSDecoder, &filename, &mode, &flags, &dont_inherit, &optimize)) { + &source, PyUnicode_FSDecoder, &filename, &mode, &flags, &dont_inherit, &optimize, &feature_version)) { goto exit; } - return_value = builtin_compile_impl(module, source, filename, mode, flags, dont_inherit, optimize); + return_value = builtin_compile_impl(module, source, filename, mode, flags, dont_inherit, optimize, feature_version); exit: return return_value; @@ -754,4 +755,4 @@ builtin_issubclass(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=54e5e33dcc2659e0 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=00b97a48ea49eaf2 input=a9049054013a1b77]*/ diff --git a/Python/compile.c b/Python/compile.c index c26210675d..697833752b 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -330,6 +330,7 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, goto finally; if (!flags) { local_flags.cf_flags = 0; + local_flags.cf_feature_version = PY_MINOR_VERSION; flags = &local_flags; } merged = c.c_future->ff_features | flags->cf_flags; diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 906877a0a8..ae6778d8a5 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -105,6 +105,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags * if (flags == NULL) { flags = &local_flags; local_flags.cf_flags = 0; + local_flags.cf_feature_version = PY_MINOR_VERSION; } v = _PySys_GetObjectId(&PyId_ps1); if (v == NULL) { @@ -1165,6 +1166,7 @@ Py_SymtableStringObject(const char *str, PyObject *filename, int start) return NULL; flags.cf_flags = 0; + flags.cf_feature_version = PY_MINOR_VERSION; mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena); if (mod == NULL) { PyArena_Free(arena); @@ -1204,6 +1206,7 @@ PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start, &iflags); if (flags == NULL) { localflags.cf_flags = 0; + localflags.cf_feature_version = PY_MINOR_VERSION; flags = &localflags; } if (n) { @@ -1249,6 +1252,7 @@ PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc, start, ps1, ps2, &err, &iflags); if (flags == NULL) { localflags.cf_flags = 0; + localflags.cf_feature_version = PY_MINOR_VERSION; flags = &localflags; } if (n) { |
