diff options
author | Guido van Rossum <guido@python.org> | 2019-03-07 12:38:08 -0800 |
---|---|---|
committer | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-03-07 12:38:08 -0800 |
commit | 495da292255b92dd73758fdd0e4c7d27d82b1e57 (patch) | |
tree | 1378cf049d2d125593fa970ea1e9a9f77604fab1 /Python/pythonrun.c | |
parent | bf94cc7b496a379e1f604aa2e4080bb70ca4020e (diff) | |
download | cpython-git-495da292255b92dd73758fdd0e4c7d27d82b1e57.tar.gz |
bpo-35975: Support parsing earlier minor versions of Python 3 (GH-12086)
This adds a `feature_version` flag to `ast.parse()` (documented) and `compile()` (hidden) that allow tweaking the parser to support older versions of the grammar. In particular if `feature_version` is 5 or 6, the hacks for the `async` and `await` keyword from PEP 492 are reinstated. (For 7 or higher, these are unconditionally treated as keywords, but they are still special tokens rather than `NAME` tokens that the parser driver recognizes.)
https://bugs.python.org/issue35975
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r-- | Python/pythonrun.c | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 906877a0a8..199ea82434 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); @@ -1198,12 +1200,15 @@ PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start, PyCompilerFlags localflags; perrdetail err; int iflags = PARSER_FLAGS(flags); + if (flags && flags->cf_feature_version < 7) + iflags |= PyPARSE_ASYNC_HACKS; node *n = PyParser_ParseStringObject(s, filename, &_PyParser_Grammar, start, &err, &iflags); if (flags == NULL) { localflags.cf_flags = 0; + localflags.cf_feature_version = PY_MINOR_VERSION; flags = &localflags; } if (n) { @@ -1249,6 +1254,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) { |