mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
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.
This commit is contained in:
parent
e5e56328af
commit
13d52c2686
15 changed files with 300 additions and 709 deletions
33
Lib/ast.py
33
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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue