mirror of
https://github.com/python/cpython.git
synced 2025-08-14 22:01:08 +00:00
bpo-35766: Change format for feature_version to (major, minor) (GH-13992) (GH-13993)
(A single int is still allowed, but undocumented.)
https://bugs.python.org/issue35766
(cherry picked from commit 10b55c1643
)
Co-authored-by: Guido van Rossum <guido@python.org>
This commit is contained in:
parent
36eea7af48
commit
3ba21070c6
4 changed files with 19 additions and 10 deletions
|
@ -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
|
Apart from the node classes, the :mod:`ast` module defines these utility functions
|
||||||
and classes for traversing abstract syntax trees:
|
and classes for traversing abstract syntax trees:
|
||||||
|
|
||||||
.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=-1)
|
.. function:: parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None)
|
||||||
|
|
||||||
Parse the source into an AST node. Equivalent to ``compile(source,
|
Parse the source into an AST node. Equivalent to ``compile(source,
|
||||||
filename, mode, ast.PyCF_ONLY_AST)``.
|
filename, mode, ast.PyCF_ONLY_AST)``.
|
||||||
|
@ -145,11 +145,12 @@ and classes for traversing abstract syntax trees:
|
||||||
modified to correspond to :pep:`484` "signature type comments",
|
modified to correspond to :pep:`484` "signature type comments",
|
||||||
e.g. ``(str, int) -> List[str]``.
|
e.g. ``(str, int) -> List[str]``.
|
||||||
|
|
||||||
Also, setting ``feature_version`` to the minor version of an
|
Also, setting ``feature_version`` to a tuple ``(major, minor)``
|
||||||
earlier Python 3 version will attempt to parse using that version's
|
will attempt to parse using that Python version's grammar.
|
||||||
grammar. For example, setting ``feature_version=4`` will allow
|
Currently ``major`` must equal to ``3``. For example, setting
|
||||||
the use of ``async`` and ``await`` as variable names. The lowest
|
``feature_version=(3, 4)`` will allow the use of ``async`` and
|
||||||
supported value is 4; the highest is ``sys.version_info[1]``.
|
``await`` as variable names. The lowest supported version is
|
||||||
|
``(3, 4)``; the highest is ``sys.version_info[0:2]``.
|
||||||
|
|
||||||
.. warning::
|
.. warning::
|
||||||
It is possible to crash the Python interpreter with a
|
It is possible to crash the Python interpreter with a
|
||||||
|
|
|
@ -352,9 +352,9 @@ The :func:`ast.parse` function has some new flags:
|
||||||
* ``mode='func_type'`` can be used to parse :pep:`484` "signature type
|
* ``mode='func_type'`` can be used to parse :pep:`484` "signature type
|
||||||
comments" (returned for function definition AST nodes);
|
comments" (returned for function definition AST nodes);
|
||||||
|
|
||||||
* ``feature_version=N`` allows specifying the minor version of an
|
* ``feature_version=(3, N)`` allows specifying an earlier Python 3
|
||||||
earlier Python 3 version. (For example, ``feature_version=4`` will
|
version. (For example, ``feature_version=(3, 4)`` will treat
|
||||||
treat ``async`` and ``await`` as non-reserved words.)
|
``async`` and ``await`` as non-reserved words.)
|
||||||
|
|
||||||
New function :func:`ast.get_source_segment` returns the source code
|
New function :func:`ast.get_source_segment` returns the source code
|
||||||
for a specific AST node.
|
for a specific AST node.
|
||||||
|
|
|
@ -28,7 +28,7 @@ from _ast import *
|
||||||
|
|
||||||
|
|
||||||
def parse(source, filename='<unknown>', mode='exec', *,
|
def parse(source, filename='<unknown>', mode='exec', *,
|
||||||
type_comments=False, feature_version=-1):
|
type_comments=False, feature_version=None):
|
||||||
"""
|
"""
|
||||||
Parse the source into an AST node.
|
Parse the source into an AST node.
|
||||||
Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
|
Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
|
||||||
|
@ -37,6 +37,13 @@ def parse(source, filename='<unknown>', mode='exec', *,
|
||||||
flags = PyCF_ONLY_AST
|
flags = PyCF_ONLY_AST
|
||||||
if type_comments:
|
if type_comments:
|
||||||
flags |= PyCF_TYPE_COMMENTS
|
flags |= PyCF_TYPE_COMMENTS
|
||||||
|
if isinstance(feature_version, tuple):
|
||||||
|
major, minor = feature_version # Should be a 2-tuple.
|
||||||
|
assert major == 3
|
||||||
|
feature_version = minor
|
||||||
|
elif feature_version is None:
|
||||||
|
feature_version = -1
|
||||||
|
# Else it should be an int giving the minor version for 3.x.
|
||||||
return compile(source, filename, mode, flags,
|
return compile(source, filename, mode, flags,
|
||||||
feature_version=feature_version)
|
feature_version=feature_version)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Change the format of feature_version to be a (major, minor) tuple.
|
Loading…
Add table
Add a link
Reference in a new issue