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
This commit is contained in:
Guido van Rossum 2019-03-07 12:38:08 -08:00 committed by Miss Islington (bot)
parent bf94cc7b49
commit 495da29225
29 changed files with 476 additions and 201 deletions

View file

@ -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,8 @@ 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,
feature_version=feature_version)
def literal_eval(node_or_string):