Skip to content

Commit

Permalink
unify some ast.argument's attrs; change Attribute column offset (closes
Browse files Browse the repository at this point in the history
python#16795)

Patch from Sven Brauch.
  • Loading branch information
benjaminp committed Mar 18, 2013
1 parent c45e041 commit cda75be
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 219 deletions.
19 changes: 9 additions & 10 deletions Include/Python-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,18 @@ struct _excepthandler {

struct _arguments {
asdl_seq *args;
identifier vararg;
expr_ty varargannotation;
arg_ty vararg;
asdl_seq *kwonlyargs;
identifier kwarg;
expr_ty kwargannotation;
asdl_seq *defaults;
asdl_seq *kw_defaults;
arg_ty kwarg;
asdl_seq *defaults;
};

struct _arg {
identifier arg;
expr_ty annotation;
int lineno;
int col_offset;
};

struct _keyword {
Expand Down Expand Up @@ -550,11 +550,10 @@ comprehension_ty _Py_comprehension(expr_ty target, expr_ty iter, asdl_seq *
excepthandler_ty _Py_ExceptHandler(expr_ty type, identifier name, asdl_seq *
body, int lineno, int col_offset, PyArena
*arena);
#define arguments(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_arguments(a0, a1, a2, a3, a4, a5, a6, a7, a8)
arguments_ty _Py_arguments(asdl_seq * args, identifier vararg, expr_ty
varargannotation, asdl_seq * kwonlyargs, identifier
kwarg, expr_ty kwargannotation, asdl_seq * defaults,
asdl_seq * kw_defaults, PyArena *arena);
#define arguments(a0, a1, a2, a3, a4, a5, a6) _Py_arguments(a0, a1, a2, a3, a4, a5, a6)
arguments_ty _Py_arguments(asdl_seq * args, arg_ty vararg, asdl_seq *
kwonlyargs, asdl_seq * kw_defaults, arg_ty kwarg,
asdl_seq * defaults, PyArena *arena);
#define arg(a0, a1, a2) _Py_arg(a0, a1, a2)
arg_ty _Py_arg(identifier arg, expr_ty annotation, PyArena *arena);
#define keyword(a0, a1, a2) _Py_keyword(a0, a1, a2)
Expand Down
82 changes: 45 additions & 37 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,36 @@ def to_tuple(t):

class AST_Tests(unittest.TestCase):

def _assertTrueorder(self, ast_node, parent_pos):
def _assertTrueorder(self, ast_node, parent_pos, reverse_check = False):
def should_reverse_check(parent, child):
# In some situations, the children of nodes occur before
# their parents, for example in a.b.c, a occurs before b
# but a is a child of b.
if isinstance(parent, ast.Call):
if parent.func == child:
return True
if isinstance(parent, (ast.Attribute, ast.Subscript)):
return True
return False

if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
return
if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
node_pos = (ast_node.lineno, ast_node.col_offset)
self.assertTrue(node_pos >= parent_pos)
if reverse_check:
self.assertTrue(node_pos <= parent_pos)
else:
self.assertTrue(node_pos >= parent_pos)
parent_pos = (ast_node.lineno, ast_node.col_offset)
for name in ast_node._fields:
value = getattr(ast_node, name)
if isinstance(value, list):
for child in value:
self._assertTrueorder(child, parent_pos)
self._assertTrueorder(child, parent_pos,
should_reverse_check(ast_node, child))
elif value is not None:
self._assertTrueorder(value, parent_pos)
self._assertTrueorder(value, parent_pos,
should_reverse_check(ast_node, value))

def test_AST_objects(self):
x = ast.AST()
Expand Down Expand Up @@ -262,14 +278,14 @@ def test_field_attr_existence(self):

def test_arguments(self):
x = ast.arguments()
self.assertEqual(x._fields, ('args', 'vararg', 'varargannotation',
'kwonlyargs', 'kwarg', 'kwargannotation',
'defaults', 'kw_defaults'))
self.assertEqual(x._fields, ('args', 'vararg',
'kwonlyargs', 'kw_defaults',
'kwarg', 'defaults'))

with self.assertRaises(AttributeError):
x.vararg

x = ast.arguments(*range(1, 9))
x = ast.arguments(*range(1, 7))
self.assertEqual(x.vararg, 2)

def test_field_attr_writable(self):
Expand Down Expand Up @@ -439,7 +455,7 @@ def test_dump(self):
"lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
"lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
"col_offset=11)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0)])"
"lineno=1, col_offset=4), lineno=1, col_offset=0)])"
)

def test_copy_location(self):
Expand All @@ -460,7 +476,7 @@ def test_fix_missing_locations(self):
"Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
"lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
"col_offset=6)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0), "
"lineno=1, col_offset=5), lineno=1, col_offset=0), "
"Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
"col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
"keywords=[], starargs=None, kwargs=None, lineno=1, "
Expand Down Expand Up @@ -560,8 +576,8 @@ def test_module(self):
self.mod(m, "must have Load context", "eval")

def _check_arguments(self, fac, check):
def arguments(args=None, vararg=None, varargannotation=None,
kwonlyargs=None, kwarg=None, kwargannotation=None,
def arguments(args=None, vararg=None,
kwonlyargs=None, kwarg=None,
defaults=None, kw_defaults=None):
if args is None:
args = []
Expand All @@ -571,20 +587,12 @@ def arguments(args=None, vararg=None, varargannotation=None,
defaults = []
if kw_defaults is None:
kw_defaults = []
args = ast.arguments(args, vararg, varargannotation, kwonlyargs,
kwarg, kwargannotation, defaults, kw_defaults)
args = ast.arguments(args, vararg, kwonlyargs, kw_defaults,
kwarg, defaults)
return fac(args)
args = [ast.arg("x", ast.Name("x", ast.Store()))]
check(arguments(args=args), "must have Load context")
check(arguments(varargannotation=ast.Num(3)),
"varargannotation but no vararg")
check(arguments(varargannotation=ast.Name("x", ast.Store()), vararg="x"),
"must have Load context")
check(arguments(kwonlyargs=args), "must have Load context")
check(arguments(kwargannotation=ast.Num(42)),
"kwargannotation but no kwarg")
check(arguments(kwargannotation=ast.Name("x", ast.Store()),
kwarg="x"), "must have Load context")
check(arguments(defaults=[ast.Num(3)]),
"more positional defaults than args")
check(arguments(kw_defaults=[ast.Num(4)]),
Expand All @@ -599,7 +607,7 @@ def arguments(args=None, vararg=None, varargannotation=None,
"must have Load context")

def test_funcdef(self):
a = ast.arguments([], None, None, [], None, None, [], [])
a = ast.arguments([], None, [], [], None, [])
f = ast.FunctionDef("x", a, [], [], None)
self.stmt(f, "empty body on FunctionDef")
f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())],
Expand Down Expand Up @@ -770,7 +778,7 @@ def test_unaryop(self):
self.expr(u, "must have Load context")

def test_lambda(self):
a = ast.arguments([], None, None, [], None, None, [], [])
a = ast.arguments([], None, [], [], None, [])
self.expr(ast.Lambda(a, ast.Name("x", ast.Store())),
"must have Load context")
def fac(args):
Expand Down Expand Up @@ -963,15 +971,15 @@ def main():
#### EVERYTHING BELOW IS GENERATED #####
exec_results = [
('Module', [('Expr', (1, 0), ('NameConstant', (1, 0), None))]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Pass', (1, 9))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', 'a', None)], None, None, [], None, None, [], []), [('Pass', (1, 10))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', 'a', None)], None, None, [], None, None, [('Num', (1, 8), 0)], []), [('Pass', (1, 12))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], 'args', None, [], None, None, [], []), [('Pass', (1, 14))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], 'kwargs', None, [], []), [('Pass', (1, 17))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', 'a', None), ('arg', 'b', None), ('arg', 'c', None), ('arg', 'd', None), ('arg', 'e', None)], 'args', None, [], 'kwargs', None, [('Num', (1, 11), 1), ('NameConstant', (1, 16), None), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])], []), [('Pass', (1, 52))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Pass', (1, 9))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, []), [('Pass', (1, 10))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None)], None, [], [], None, [('Num', (1, 8), 0)]), [('Pass', (1, 12))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], ('arg', (1, 7), 'args', None), [], [], None, []), [('Pass', (1, 14))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], ('arg', (1, 8), 'kwargs', None), []), [('Pass', (1, 17))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None), ('arg', (1, 9), 'b', None), ('arg', (1, 14), 'c', None), ('arg', (1, 22), 'd', None), ('arg', (1, 28), 'e', None)], ('arg', (1, 35), 'args', None), [], [], ('arg', (1, 43), 'kwargs', None), [('Num', (1, 11), 1), ('NameConstant', (1, 16), None), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Pass', (1, 52))], [], None)]),
('Module', [('ClassDef', (1, 0), 'C', [], [], None, None, [('Pass', (1, 8))], [])]),
('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], None, None, [('Pass', (1, 17))], [])]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, None, [], None, None, [], []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]),
('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
('Module', [('AugAssign', (1, 0), ('Name', (1, 0), 'v', ('Store',)), ('Add',), ('Num', (1, 5), 1))]),
Expand All @@ -980,7 +988,7 @@ def main():
('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))])]),
('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',))), ('withitem', ('Name', (1, 13), 'z', ('Load',)), ('Name', (1, 18), 'q', ('Store',)))], [('Pass', (1, 21))])]),
('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], [], None, None), None)]),
('Module', [('Raise', (1, 0), ('Call', (1, 15), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], [], None, None), None)]),
('Module', [('Try', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [], [])]),
('Module', [('Try', (1, 0), [('Pass', (2, 2))], [], [], [('Pass', (4, 2))])]),
('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
Expand Down Expand Up @@ -1009,25 +1017,25 @@ def main():
('Expression', ('BoolOp', (1, 0), ('And',), [('Name', (1, 0), 'a', ('Load',)), ('Name', (1, 6), 'b', ('Load',))])),
('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
('Expression', ('Lambda', (1, 0), ('arguments', [], None, None, [], None, None, [], []), ('NameConstant', (1, 7), None))),
('Expression', ('Lambda', (1, 0), ('arguments', [], None, [], [], None, []), ('NameConstant', (1, 7), None))),
('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
('Expression', ('Dict', (1, 0), [], [])),
('Expression', ('Set', (1, 0), [('NameConstant', (1, 1), None)])),
('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2)], [('keyword', 'c', ('Num', (1, 8), 3))], ('Name', (1, 11), 'd', ('Load',)), ('Name', (1, 15), 'e', ('Load',)))),
('Expression', ('Call', (1, 1), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2)], [('keyword', 'c', ('Num', (1, 8), 3))], ('Name', (1, 11), 'd', ('Load',)), ('Name', (1, 15), 'e', ('Load',)))),
('Expression', ('Num', (1, 0), 10)),
('Expression', ('Str', (1, 0), 'string')),
('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
('Expression', ('Subscript', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
('Expression', ('Attribute', (1, 2), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
('Expression', ('Subscript', (1, 2), ('Name', (1, 0), 'a', ('Load',)), ('Slice', ('Name', (1, 2), 'b', ('Load',)), ('Name', (1, 4), 'c', ('Load',)), None), ('Load',))),
('Expression', ('Name', (1, 0), 'v', ('Load',))),
('Expression', ('List', (1, 0), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
('Expression', ('List', (1, 0), [], ('Load',))),
('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
('Expression', ('Tuple', (1, 0), [], ('Load',))),
('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [], None, None)),
('Expression', ('Call', (1, 7), ('Attribute', (1, 6), ('Attribute', (1, 4), ('Attribute', (1, 2), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 12), ('Attribute', (1, 10), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [], None, None)),
]
main()
4 changes: 4 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ What's New in Python 3.4.0 Alpha 1?
Core and Builtins
-----------------

- Issue #16795: On the ast.arguments object, unify vararg with varargannotation
and kwarg and kwargannotation. Change the column offset of ast.Attribute to be
at the attribute name.

- Issue #17434: Properly raise a SyntaxError when a string occurs between future
imports.

Expand Down
8 changes: 4 additions & 4 deletions Parser/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ module Python
excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body)
attributes (int lineno, int col_offset)

arguments = (arg* args, identifier? vararg, expr? varargannotation,
arg* kwonlyargs, identifier? kwarg,
expr? kwargannotation, expr* defaults,
expr* kw_defaults)
arguments = (arg* args, arg? vararg, arg* kwonlyargs, expr* kw_defaults,
arg? kwarg, expr* defaults)

arg = (identifier arg, expr? annotation)
attributes (int lineno, int col_offset)

-- keyword arguments supplied to call
keyword = (identifier arg, expr value)
Expand Down
18 changes: 15 additions & 3 deletions Parser/asdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,19 @@ def p_type_2(self, info):
msg="expected attributes, found %s" % id)
return Sum(sum, attributes)

def p_product(self, info):
def p_product_0(self, info):
" product ::= ( fields ) "
_0, fields, _1 = info
return Product(fields)

def p_product_1(self, info):
" product ::= ( fields ) Id ( fields ) "
_0, fields, _1, id, _2, attributes, _3 = info
if id.value != "attributes":
raise ASDLSyntaxError(id.lineno,
msg="expected attributes, found %s" % id)
return Product(fields, attributes)

def p_sum_0(self, constructor):
" sum ::= constructor "
return [constructor[0]]
Expand Down Expand Up @@ -289,11 +297,15 @@ def __repr__(self):
return "Sum(%s, %s)" % (self.types, self.attributes)

class Product(AST):
def __init__(self, fields):
def __init__(self, fields, attributes=None):
self.fields = fields
self.attributes = attributes or []

def __repr__(self):
return "Product(%s)" % self.fields
if self.attributes is None:
return "Product(%s)" % self.fields
else:
return "Product(%s, %s)" % (self.fields, self.attributes)

class VisitorBase(object):

Expand Down
Loading

0 comments on commit cda75be

Please sign in to comment.