summaryrefslogtreecommitdiffstats
path: root/Doc/library/ast.rst
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2019-03-07 20:38:08 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-03-07 20:38:08 (GMT)
commit495da292255b92dd73758fdd0e4c7d27d82b1e57 (patch)
tree1378cf049d2d125593fa970ea1e9a9f77604fab1 /Doc/library/ast.rst
parentbf94cc7b496a379e1f604aa2e4080bb70ca4020e (diff)
downloadcpython-495da292255b92dd73758fdd0e4c7d27d82b1e57.zip
cpython-495da292255b92dd73758fdd0e4c7d27d82b1e57.tar.gz
cpython-495da292255b92dd73758fdd0e4c7d27d82b1e57.tar.bz2
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 'Doc/library/ast.rst')
-rw-r--r--Doc/library/ast.rst10
1 files changed, 8 insertions, 2 deletions
diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst
index 3df7f9e..1884bea 100644
--- a/Doc/library/ast.rst
+++ b/Doc/library/ast.rst
@@ -126,7 +126,7 @@ The abstract grammar is currently defined as follows:
Apart from the node classes, the :mod:`ast` module defines these utility functions
and classes for traversing abstract syntax trees:
-.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False)
+.. function:: 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, ast.PyCF_ONLY_AST)``.
@@ -145,13 +145,19 @@ and classes for traversing abstract syntax trees:
modified to correspond to :pep:`484` "signature type comments",
e.g. ``(str, int) -> List[str]``.
+ Also, setting ``feature_version`` to the minor version of an
+ earlier Python 3 version will attempt to parse using that version's
+ grammar. For example, setting ``feature_version=4`` will allow
+ the use of ``async`` and ``await`` as variable names. The lowest
+ supported value is 4; the highest is ``sys.version_info[1]``.
+
.. warning::
It is possible to crash the Python interpreter with a
sufficiently large/complex string due to stack depth limitations
in Python's AST compiler.
.. versionchanged:: 3.8
- Added ``type_comments=True`` and ``mode='func_type'``.
+ Added ``type_comments``, ``mode='func_type'`` and ``feature_version``.
.. function:: literal_eval(node_or_string)