mirror of
https://github.com/python/cpython.git
synced 2025-11-01 10:45:30 +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
|
|
@ -343,6 +343,10 @@ class AST_Tests(unittest.TestCase):
|
|||
def test_field_attr_existence(self):
|
||||
for name, item in ast.__dict__.items():
|
||||
if self._is_ast_node(name, item):
|
||||
if name == 'Index':
|
||||
# Index(value) just returns value now.
|
||||
# The argument is required.
|
||||
continue
|
||||
x = item()
|
||||
if isinstance(x, ast.AST):
|
||||
self.assertEqual(type(x._fields), tuple)
|
||||
|
|
@ -1308,11 +1312,11 @@ class ASTValidatorTests(unittest.TestCase):
|
|||
self.expr(attr, "must have Load context")
|
||||
|
||||
def test_subscript(self):
|
||||
sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Index(ast.Num(3)),
|
||||
sub = ast.Subscript(ast.Name("x", ast.Store()), ast.Num(3),
|
||||
ast.Load())
|
||||
self.expr(sub, "must have Load context")
|
||||
x = ast.Name("x", ast.Load())
|
||||
sub = ast.Subscript(x, ast.Index(ast.Name("y", ast.Store())),
|
||||
sub = ast.Subscript(x, ast.Name("y", ast.Store()),
|
||||
ast.Load())
|
||||
self.expr(sub, "must have Load context")
|
||||
s = ast.Name("x", ast.Store())
|
||||
|
|
@ -1320,9 +1324,9 @@ class ASTValidatorTests(unittest.TestCase):
|
|||
sl = ast.Slice(*args)
|
||||
self.expr(ast.Subscript(x, sl, ast.Load()),
|
||||
"must have Load context")
|
||||
sl = ast.ExtSlice([])
|
||||
self.expr(ast.Subscript(x, sl, ast.Load()), "empty dims on ExtSlice")
|
||||
sl = ast.ExtSlice([ast.Index(s)])
|
||||
sl = ast.Tuple([], ast.Load())
|
||||
self.expr(ast.Subscript(x, sl, ast.Load()))
|
||||
sl = ast.Tuple([s], ast.Load())
|
||||
self.expr(ast.Subscript(x, sl, ast.Load()), "must have Load context")
|
||||
|
||||
def test_starred(self):
|
||||
|
|
@ -1664,11 +1668,11 @@ class EndPositionTests(unittest.TestCase):
|
|||
''').strip()
|
||||
i1, i2, im = map(self._parse_value, (s1, s2, sm))
|
||||
self._check_content(s1, i1.value, 'f()[1, 2]')
|
||||
self._check_content(s1, i1.value.slice.value, '1, 2')
|
||||
self._check_content(s1, i1.value.slice, '1, 2')
|
||||
self._check_content(s2, i2.slice.lower, 'a.b')
|
||||
self._check_content(s2, i2.slice.upper, 'c.d')
|
||||
self._check_content(sm, im.slice.dims[0].upper, 'f ()')
|
||||
self._check_content(sm, im.slice.dims[1].lower, 'g ()')
|
||||
self._check_content(sm, im.slice.elts[0].upper, 'f ()')
|
||||
self._check_content(sm, im.slice.elts[1].lower, 'g ()')
|
||||
self._check_end_pos(im, 3, 3)
|
||||
|
||||
def test_binop(self):
|
||||
|
|
@ -1989,13 +1993,13 @@ eval_results = [
|
|||
('Expression', ('Constant', (1, 0, 1, 2), 10, None)),
|
||||
('Expression', ('Constant', (1, 0, 1, 8), 'string', None)),
|
||||
('Expression', ('Attribute', (1, 0, 1, 3), ('Name', (1, 0, 1, 1), 'a', ('Load',)), 'b', ('Load',))),
|
||||
('Expression', ('Subscript', (1, 0, 1, 6), ('Name', (1, 0, 1, 1), 'a', ('Load',)), ('Slice', ('Name', (1, 2, 1, 3), 'b', ('Load',)), ('Name', (1, 4, 1, 5), 'c', ('Load',)), None), ('Load',))),
|
||||
('Expression', ('Subscript', (1, 0, 1, 6), ('Name', (1, 0, 1, 1), 'a', ('Load',)), ('Slice', (1, 2, 1, 5), ('Name', (1, 2, 1, 3), 'b', ('Load',)), ('Name', (1, 4, 1, 5), 'c', ('Load',)), None), ('Load',))),
|
||||
('Expression', ('Name', (1, 0, 1, 1), 'v', ('Load',))),
|
||||
('Expression', ('List', (1, 0, 1, 7), [('Constant', (1, 1, 1, 2), 1, None), ('Constant', (1, 3, 1, 4), 2, None), ('Constant', (1, 5, 1, 6), 3, None)], ('Load',))),
|
||||
('Expression', ('List', (1, 0, 1, 2), [], ('Load',))),
|
||||
('Expression', ('Tuple', (1, 0, 1, 5), [('Constant', (1, 0, 1, 1), 1, None), ('Constant', (1, 2, 1, 3), 2, None), ('Constant', (1, 4, 1, 5), 3, None)], ('Load',))),
|
||||
('Expression', ('Tuple', (1, 0, 1, 7), [('Constant', (1, 1, 1, 2), 1, None), ('Constant', (1, 3, 1, 4), 2, None), ('Constant', (1, 5, 1, 6), 3, None)], ('Load',))),
|
||||
('Expression', ('Tuple', (1, 0, 1, 2), [], ('Load',))),
|
||||
('Expression', ('Call', (1, 0, 1, 17), ('Attribute', (1, 0, 1, 7), ('Attribute', (1, 0, 1, 5), ('Attribute', (1, 0, 1, 3), ('Name', (1, 0, 1, 1), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8, 1, 16), ('Attribute', (1, 8, 1, 11), ('Name', (1, 8, 1, 9), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Constant', (1, 12, 1, 13), 1, None), ('Constant', (1, 14, 1, 15), 2, None), None), ('Load',))], [])),
|
||||
('Expression', ('Call', (1, 0, 1, 17), ('Attribute', (1, 0, 1, 7), ('Attribute', (1, 0, 1, 5), ('Attribute', (1, 0, 1, 3), ('Name', (1, 0, 1, 1), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8, 1, 16), ('Attribute', (1, 8, 1, 11), ('Name', (1, 8, 1, 9), 'a', ('Load',)), 'b', ('Load',)), ('Slice', (1, 12, 1, 15), ('Constant', (1, 12, 1, 13), 1, None), ('Constant', (1, 14, 1, 15), 2, None), None), ('Load',))], [])),
|
||||
]
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue