bpo-34822: Simplify AST for subscription. (GH-9605) · python/cpython@13d52c2 · GitHub
Skip to content

Commit 13d52c2

Browse files
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.
1 parent e5e5632 commit 13d52c2

15 files changed

Lines changed: 293 additions & 702 deletions

File tree

Doc/library/ast.rst

Lines changed: 31 additions & 37 deletions

Doc/tools/susp-ignored.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ howto/pyporting,,::,Programming Language :: Python :: 3
108108
howto/regex,,::,
109109
howto/regex,,:foo,(?:foo)
110110
howto/urllib2,,:password,"""joe:password@example.com"""
111+
library/ast,,:upper,lower:upper
112+
library/ast,,:step,lower:upper:step
111113
library/audioop,,:ipos,"# factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],"
112114
library/bisect,32,:hi,all(val >= x for val in a[i:hi])
113115
library/bisect,42,:hi,all(val > x for val in a[i:hi])

Doc/whatsnew/3.9.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,12 @@ Deprecated
535535

536536
(Contributed by Victor Stinner in :issue:`39353`.)
537537

538+
* :mod:`ast` classes ``Index`` and ``ExtSlice`` are considered deprecated
539+
and will be removed in future Python versions. ``value`` itself should be
540+
used instead of ``Index(value)``. ``Tuple(slices, Load())`` should be
541+
used instead of ``ExtSlice(slices)``.
542+
(Contributed by Serhiy Storchaka in :issue:`32892`.)
543+
538544
* The :c:func:`PyEval_InitThreads` and :c:func:`PyEval_ThreadsInitialized`
539545
functions are now deprecated and will be removed in Python 3.11. Calling
540546
:c:func:`PyEval_InitThreads` now does nothing. The :term:`GIL` is initialized
@@ -667,10 +673,17 @@ Changes in the Python API
667673
since the *buffering* parameter has been removed.
668674
(Contributed by Victor Stinner in :issue:`39357`.)
669675

676+
* Simplified AST for subscription. Simple indices will be represented by
677+
their value, extended slices will be represented as tuples.
678+
``Index(value)`` will return a ``value`` itself, ``ExtSlice(slices)``
679+
will return ``Tuple(slices, Load())``.
680+
(Contributed by Serhiy Storchaka in :issue:`34822`.)
681+
670682
* The :mod:`importlib` module now ignores the :envvar:`PYTHONCASEOK`
671683
environment variable when the :option:`-E` or :option:`-I` command line
672684
options are being used.
673685

686+
674687
CPython bytecode changes
675688
------------------------
676689

Include/Python-ast.h

Lines changed: 11 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/ast.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ class RewriteName(NodeTransformer):
445445
def visit_Name(self, node):
446446
return copy_location(Subscript(
447447
value=Name(id='data', ctx=Load()),
448-
slice=Index(value=Str(s=node.id)),
448+
slice=Constant(value=node.id),
449449
ctx=node.ctx
450450
), node)
451451
@@ -552,6 +552,7 @@ def __new__(cls, *args, **kwargs):
552552
_const_types_not = {
553553
Num: (bool,),
554554
}
555+
555556
_const_node_type_names = {
556557
bool: 'NameConstant', # should be before int
557558
type(None): 'NameConstant',
@@ -563,6 +564,23 @@ def __new__(cls, *args, **kwargs):
563564
type(...): 'Ellipsis',
564565
}
565566

567+
class Index(AST):
568+
def __new__(cls, value, **kwargs):
569+
return value
570+
571+
class ExtSlice(AST):
572+
def __new__(cls, dims=(), **kwargs):
573+
return Tuple(list(dims), Load(), **kwargs)
574+
575+
def _dims_getter(self):
576+
return self.elts
577+
578+
def _dims_setter(self, value):
579+
self.elts = value
580+
581+
Tuple.dims = property(_dims_getter, _dims_setter)
582+
583+
566584
# Large float and imaginary literals get turned into infinities in the AST.
567585
# We unparse those infinities to INFSTR.
568586
_INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
@@ -1268,10 +1286,8 @@ def visit_Subscript(self, node):
12681286
self.set_precedence(_Precedence.ATOM, node.value)
12691287
self.traverse(node.value)
12701288
with self.delimit("[", "]"):
1271-
if (isinstance(node.slice, Index)
1272-
and isinstance(node.slice.value, Tuple)
1273-
and node.slice.value.elts):
1274-
self.items_view(self.traverse, node.slice.value.elts)
1289+
if isinstance(node.slice, Tuple) and node.slice.elts:
1290+
self.items_view(self.traverse, node.slice.elts)
12751291
else:
12761292
self.traverse(node.slice)
12771293

@@ -1283,10 +1299,6 @@ def visit_Starred(self, node):
12831299
def visit_Ellipsis(self, node):
12841300
self.write("...")
12851301

1286-
def visit_Index(self, node):
1287-
self.set_precedence(_Precedence.TUPLE, node.value)
1288-
self.traverse(node.value)
1289-
12901302
def visit_Slice(self, node):
12911303
if node.lower:
12921304
self.traverse(node.lower)
@@ -1297,9 +1309,6 @@ def visit_Slice(self, node):
12971309
self.write(":")
12981310
self.traverse(node.step)
12991311

1300-
def visit_ExtSlice(self, node):
1301-
self.items_view(self.traverse, node.dims)
1302-
13031312
def visit_arg(self, node):
13041313
self.write(node.arg)
13051314
if node.annotation:

Lib/test/test_ast.py

Lines changed: 14 additions & 10 deletions

0 commit comments

Comments
 (0)