summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-03-10 16:52:34 (GMT)
committerGitHub <noreply@github.com>2020-03-10 16:52:34 (GMT)
commit13d52c268699f199a8e917a0f1dc4c51e5346c42 (patch)
treed602c97d77e3222d38c6300ed822021e51bd9dce /Doc
parente5e56328afac50aad6d8893185d8e7ba8928afe2 (diff)
downloadcpython-13d52c268699f199a8e917a0f1dc4c51e5346c42.zip
cpython-13d52c268699f199a8e917a0f1dc4c51e5346c42.tar.gz
cpython-13d52c268699f199a8e917a0f1dc4c51e5346c42.tar.bz2
bpo-34822: Simplify AST for subscription. (GH-9605)
* Remove the slice type. * Make Slice a kind of the expr type instead of the slice type. * Replace ExtSlice(slices) with Tuple(slices, Load()). * Replace Index(value) with a value itself. All non-terminal nodes in AST for expressions are now of the expr type.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/ast.rst68
-rw-r--r--Doc/tools/susp-ignored.csv2
-rw-r--r--Doc/whatsnew/3.9.rst13
3 files changed, 46 insertions, 37 deletions
diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst
index a11f8b9..45f16c1 100644
--- a/Doc/library/ast.rst
+++ b/Doc/library/ast.rst
@@ -120,13 +120,26 @@ Node classes
Class :class:`ast.Constant` is now used for all constants.
+.. versionchanged:: 3.9
+
+ Simple indices are represented by their value, extended slices are
+ represented as tuples.
+
.. deprecated:: 3.8
Old classes :class:`ast.Num`, :class:`ast.Str`, :class:`ast.Bytes`,
:class:`ast.NameConstant` and :class:`ast.Ellipsis` are still available,
- but they will be removed in future Python releases. In the meanwhile,
+ but they will be removed in future Python releases. In the meantime,
instantiating them will return an instance of a different class.
+.. deprecated:: 3.9
+
+ Old classes :class:`ast.Index` and :class:`ast.ExtSlice` are still
+ available, but they will be removed in future Python releases.
+ In the meantime, instantiating them will return an instance of
+ a different class.
+
+
Literals
^^^^^^^^
@@ -552,30 +565,33 @@ Subscripting
.. class:: Subscript(value, slice, ctx)
- A subscript, such as ``l[1]``. ``value`` is the object, often a
- :class:`Name`. ``slice`` is one of :class:`Index`, :class:`Slice` or
- :class:`ExtSlice`. ``ctx`` is :class:`Load`, :class:`Store` or :class:`Del`
+ A subscript, such as ``l[1]``. ``value`` is the subscripted object
+ (usually sequence or mapping). ``slice`` is an index, slice or key.
+ It can be a :class:`Tuple` and contain a :class:`Slice`.
+ ``ctx`` is :class:`Load`, :class:`Store` or :class:`Del`
according to the action performed with the subscript.
-
-.. class:: Index(value)
-
- Simple subscripting with a single value
-
.. doctest::
- >>> print(ast.dump(ast.parse('l[1]', mode='eval'), indent=4))
+ >>> print(ast.dump(ast.parse('l[1:2, 3]', mode='eval'), indent=4))
Expression(
body=Subscript(
value=Name(id='l', ctx=Load()),
- slice=Index(
- value=Constant(value=1)),
+ slice=Tuple(
+ elts=[
+ Slice(
+ lower=Constant(value=1),
+ upper=Constant(value=2)),
+ Constant(value=3)],
+ ctx=Load()),
ctx=Load()))
.. class:: Slice(lower, upper, step)
- Regular slicing (on the form x:y).
+ Regular slicing (on the form ``lower:upper`` or ``lower:upper:step``).
+ Can occur only inside the *slice* field of :class:`Subscript`, either
+ directly or as an element of :class:`Tuple`.
.. doctest::
@@ -589,27 +605,6 @@ Subscripting
ctx=Load()))
-.. class:: ExtSlice(dims)
-
- Advanced slicing. ``dims`` holds a list of :class:`Slice` and
- :class:`Index` nodes
-
- .. doctest::
-
- >>> print(ast.dump(ast.parse('l[1:2, 3]', mode='eval'), indent=4))
- Expression(
- body=Subscript(
- value=Name(id='l', ctx=Load()),
- slice=ExtSlice(
- dims=[
- Slice(
- lower=Constant(value=1),
- upper=Constant(value=2)),
- Index(
- value=Constant(value=3))]),
- ctx=Load()))
-
-
Comprehensions
~~~~~~~~~~~~~~
@@ -823,8 +818,7 @@ Statements
AnnAssign(
target=Subscript(
value=Name(id='a', ctx=Load()),
- slice=Index(
- value=Constant(value=1)),
+ slice=Constant(value=1),
ctx=Store()),
annotation=Name(id='int', ctx=Load()),
simple=0)],
@@ -1708,7 +1702,7 @@ and classes for traversing abstract syntax trees:
def visit_Name(self, node):
return Subscript(
value=Name(id='data', ctx=Load()),
- slice=Index(value=Constant(value=node.id)),
+ slice=Constant(value=node.id),
ctx=node.ctx
), node)
diff --git a/Doc/tools/susp-ignored.csv b/Doc/tools/susp-ignored.csv
index d3901be..7be8d0a 100644
--- a/Doc/tools/susp-ignored.csv
+++ b/Doc/tools/susp-ignored.csv
@@ -108,6 +108,8 @@ howto/pyporting,,::,Programming Language :: Python :: 3
howto/regex,,::,
howto/regex,,:foo,(?:foo)
howto/urllib2,,:password,"""joe:password@example.com"""
+library/ast,,:upper,lower:upper
+library/ast,,:step,lower:upper:step
library/audioop,,:ipos,"# factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],"
library/bisect,32,:hi,all(val >= x for val in a[i:hi])
library/bisect,42,:hi,all(val > x for val in a[i:hi])
diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index 9056289..690dbba 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -535,6 +535,12 @@ Deprecated
(Contributed by Victor Stinner in :issue:`39353`.)
+* :mod:`ast` classes ``Index`` and ``ExtSlice`` are considered deprecated
+ and will be removed in future Python versions. ``value`` itself should be
+ used instead of ``Index(value)``. ``Tuple(slices, Load())`` should be
+ used instead of ``ExtSlice(slices)``.
+ (Contributed by Serhiy Storchaka in :issue:`32892`.)
+
* The :c:func:`PyEval_InitThreads` and :c:func:`PyEval_ThreadsInitialized`
functions are now deprecated and will be removed in Python 3.11. Calling
:c:func:`PyEval_InitThreads` now does nothing. The :term:`GIL` is initialized
@@ -667,10 +673,17 @@ Changes in the Python API
since the *buffering* parameter has been removed.
(Contributed by Victor Stinner in :issue:`39357`.)
+* Simplified AST for subscription. Simple indices will be represented by
+ their value, extended slices will be represented as tuples.
+ ``Index(value)`` will return a ``value`` itself, ``ExtSlice(slices)``
+ will return ``Tuple(slices, Load())``.
+ (Contributed by Serhiy Storchaka in :issue:`34822`.)
+
* The :mod:`importlib` module now ignores the :envvar:`PYTHONCASEOK`
environment variable when the :option:`-E` or :option:`-I` command line
options are being used.
+
CPython bytecode changes
------------------------