summaryrefslogtreecommitdiffstats
path: root/Lib/ast.py
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 /Lib/ast.py
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 'Lib/ast.py')
-rw-r--r--Lib/ast.py33
1 files changed, 21 insertions, 12 deletions
diff --git a/Lib/ast.py b/Lib/ast.py
index 0bce4a4..8b88d0f 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -445,7 +445,7 @@ class NodeTransformer(NodeVisitor):
def visit_Name(self, node):
return copy_location(Subscript(
value=Name(id='data', ctx=Load()),
- slice=Index(value=Str(s=node.id)),
+ slice=Constant(value=node.id),
ctx=node.ctx
), node)
@@ -552,6 +552,7 @@ _const_types = {
_const_types_not = {
Num: (bool,),
}
+
_const_node_type_names = {
bool: 'NameConstant', # should be before int
type(None): 'NameConstant',
@@ -563,6 +564,23 @@ _const_node_type_names = {
type(...): 'Ellipsis',
}
+class Index(AST):
+ def __new__(cls, value, **kwargs):
+ return value
+
+class ExtSlice(AST):
+ def __new__(cls, dims=(), **kwargs):
+ return Tuple(list(dims), Load(), **kwargs)
+
+def _dims_getter(self):
+ return self.elts
+
+def _dims_setter(self, value):
+ self.elts = value
+
+Tuple.dims = property(_dims_getter, _dims_setter)
+
+
# Large float and imaginary literals get turned into infinities in the AST.
# We unparse those infinities to INFSTR.
_INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
@@ -1268,10 +1286,8 @@ class _Unparser(NodeVisitor):
self.set_precedence(_Precedence.ATOM, node.value)
self.traverse(node.value)
with self.delimit("[", "]"):
- if (isinstance(node.slice, Index)
- and isinstance(node.slice.value, Tuple)
- and node.slice.value.elts):
- self.items_view(self.traverse, node.slice.value.elts)
+ if isinstance(node.slice, Tuple) and node.slice.elts:
+ self.items_view(self.traverse, node.slice.elts)
else:
self.traverse(node.slice)
@@ -1283,10 +1299,6 @@ class _Unparser(NodeVisitor):
def visit_Ellipsis(self, node):
self.write("...")
- def visit_Index(self, node):
- self.set_precedence(_Precedence.TUPLE, node.value)
- self.traverse(node.value)
-
def visit_Slice(self, node):
if node.lower:
self.traverse(node.lower)
@@ -1297,9 +1309,6 @@ class _Unparser(NodeVisitor):
self.write(":")
self.traverse(node.step)
- def visit_ExtSlice(self, node):
- self.items_view(self.traverse, node.dims)
-
def visit_arg(self, node):
self.write(node.arg)
if node.annotation: