unify some ast.argument's attrs; change Attribute column offset (closes #16795)
Patch from Sven Brauch.
This commit is contained in:
parent
c45e041bff
commit
cda75be02a
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
|
|
@ -180,20 +180,36 @@ eval_tests = [
|
|||
|
||||
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()
|
||||
|
@ -262,14 +278,14 @@ class AST_Tests(unittest.TestCase):
|
|||
|
||||
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):
|
||||
|
@ -439,7 +455,7 @@ class ASTHelpers_Test(unittest.TestCase):
|
|||
"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):
|
||||
|
@ -460,7 +476,7 @@ class ASTHelpers_Test(unittest.TestCase):
|
|||
"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, "
|
||||
|
@ -560,8 +576,8 @@ class ASTValidatorTests(unittest.TestCase):
|
|||
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 = []
|
||||
|
@ -571,20 +587,12 @@ class ASTValidatorTests(unittest.TestCase):
|
|||
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)]),
|
||||
|
@ -599,7 +607,7 @@ class ASTValidatorTests(unittest.TestCase):
|
|||
"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())],
|
||||
|
@ -770,7 +778,7 @@ class ASTValidatorTests(unittest.TestCase):
|
|||
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):
|
||||
|
@ -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))]),
|
||||
|
@ -980,7 +988,7 @@ exec_results = [
|
|||
('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)]),
|
||||
|
@ -1009,7 +1017,7 @@ eval_results = [
|
|||
('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)])),
|
||||
|
@ -1017,17 +1025,17 @@ eval_results = [
|
|||
('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()
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -158,11 +158,19 @@ class ASDLParser(spark.GenericParser, object):
|
|||
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]]
|
||||
|
@ -289,11 +297,15 @@ class Sum(AST):
|
|||
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):
|
||||
|
||||
|
|
|
@ -209,6 +209,11 @@ class StructVisitor(EmitVisitor):
|
|||
self.emit("struct _%(name)s {" % locals(), depth)
|
||||
for f in product.fields:
|
||||
self.visit(f, depth + 1)
|
||||
for field in product.attributes:
|
||||
# rudimentary attribute handling
|
||||
type = str(field.type)
|
||||
assert type in asdl.builtin_types, type
|
||||
self.emit("%s %s;" % (type, field.name), depth + 1);
|
||||
self.emit("};", depth)
|
||||
self.emit("", depth)
|
||||
|
||||
|
@ -493,7 +498,13 @@ class Obj2ModVisitor(PickleVisitor):
|
|||
|
||||
def visitField(self, field, name, sum=None, prod=None, depth=0):
|
||||
ctype = get_c_type(field.type)
|
||||
self.emit("if (_PyObject_HasAttrId(obj, &PyId_%s)) {" % field.name, depth)
|
||||
if field.opt:
|
||||
add_check = " && _PyObject_GetAttrId(obj, &PyId_%s) != Py_None" \
|
||||
% (field.name)
|
||||
else:
|
||||
add_check = str()
|
||||
self.emit("if (_PyObject_HasAttrId(obj, &PyId_%s)%s) {"
|
||||
% (field.name, add_check), depth, reflow=False)
|
||||
self.emit("int res;", depth+1)
|
||||
if field.seq:
|
||||
self.emit("Py_ssize_t len;", depth+1)
|
||||
|
@ -559,6 +570,13 @@ class PyTypesDeclareVisitor(PickleVisitor):
|
|||
def visitProduct(self, prod, name):
|
||||
self.emit("static PyTypeObject *%s_type;" % name, 0)
|
||||
self.emit("static PyObject* ast2obj_%s(void*);" % name, 0)
|
||||
if prod.attributes:
|
||||
for a in prod.attributes:
|
||||
self.emit_identifier(a.name)
|
||||
self.emit("static char *%s_attributes[] = {" % name, 0)
|
||||
for a in prod.attributes:
|
||||
self.emit('"%s",' % a.name, 1)
|
||||
self.emit("};", 0)
|
||||
if prod.fields:
|
||||
for f in prod.fields:
|
||||
self.emit_identifier(f.name)
|
||||
|
@ -934,6 +952,11 @@ static int add_ast_fields(void)
|
|||
self.emit('%s_type = make_type("%s", &AST_type, %s, %d);' %
|
||||
(name, name, fields, len(prod.fields)), 1)
|
||||
self.emit("if (!%s_type) return 0;" % name, 1)
|
||||
if prod.attributes:
|
||||
self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" %
|
||||
(name, name, len(prod.attributes)), 1)
|
||||
else:
|
||||
self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1)
|
||||
|
||||
def visitSum(self, sum, name):
|
||||
self.emit('%s_type = make_type("%s", &AST_type, NULL, 0);' %
|
||||
|
@ -1089,6 +1112,12 @@ class ObjVisitor(PickleVisitor):
|
|||
self.emit("if (!result) return NULL;", 1)
|
||||
for field in prod.fields:
|
||||
self.visitField(field, name, 1, True)
|
||||
for a in prod.attributes:
|
||||
self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1)
|
||||
self.emit("if (!value) goto failed;", 1)
|
||||
self.emit('if (_PyObject_SetAttrId(result, &PyId_%s, value) < 0)' % a.name, 1)
|
||||
self.emit('goto failed;', 2)
|
||||
self.emit('Py_DECREF(value);', 1)
|
||||
self.func_end()
|
||||
|
||||
def visitConstructor(self, cons, enum, name):
|
||||
|
|
|
@ -412,24 +412,24 @@ static char *ExceptHandler_fields[]={
|
|||
static PyTypeObject *arguments_type;
|
||||
static PyObject* ast2obj_arguments(void*);
|
||||
_Py_IDENTIFIER(vararg);
|
||||
_Py_IDENTIFIER(varargannotation);
|
||||
_Py_IDENTIFIER(kwonlyargs);
|
||||
_Py_IDENTIFIER(kwarg);
|
||||
_Py_IDENTIFIER(kwargannotation);
|
||||
_Py_IDENTIFIER(defaults);
|
||||
_Py_IDENTIFIER(kw_defaults);
|
||||
_Py_IDENTIFIER(kwarg);
|
||||
_Py_IDENTIFIER(defaults);
|
||||
static char *arguments_fields[]={
|
||||
"args",
|
||||
"vararg",
|
||||
"varargannotation",
|
||||
"kwonlyargs",
|
||||
"kwarg",
|
||||
"kwargannotation",
|
||||
"defaults",
|
||||
"kw_defaults",
|
||||
"kwarg",
|
||||
"defaults",
|
||||
};
|
||||
static PyTypeObject *arg_type;
|
||||
static PyObject* ast2obj_arg(void*);
|
||||
static char *arg_attributes[] = {
|
||||
"lineno",
|
||||
"col_offset",
|
||||
};
|
||||
_Py_IDENTIFIER(arg);
|
||||
_Py_IDENTIFIER(annotation);
|
||||
static char *arg_fields[]={
|
||||
|
@ -1056,6 +1056,7 @@ static int init_types(void)
|
|||
comprehension_type = make_type("comprehension", &AST_type,
|
||||
comprehension_fields, 3);
|
||||
if (!comprehension_type) return 0;
|
||||
if (!add_attributes(comprehension_type, NULL, 0)) return 0;
|
||||
excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
|
||||
if (!excepthandler_type) return 0;
|
||||
if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
|
||||
|
@ -1063,16 +1064,21 @@ static int init_types(void)
|
|||
ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
|
||||
ExceptHandler_fields, 3);
|
||||
if (!ExceptHandler_type) return 0;
|
||||
arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
|
||||
arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
|
||||
if (!arguments_type) return 0;
|
||||
if (!add_attributes(arguments_type, NULL, 0)) return 0;
|
||||
arg_type = make_type("arg", &AST_type, arg_fields, 2);
|
||||
if (!arg_type) return 0;
|
||||
if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
|
||||
keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
|
||||
if (!keyword_type) return 0;
|
||||
if (!add_attributes(keyword_type, NULL, 0)) return 0;
|
||||
alias_type = make_type("alias", &AST_type, alias_fields, 2);
|
||||
if (!alias_type) return 0;
|
||||
if (!add_attributes(alias_type, NULL, 0)) return 0;
|
||||
withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
|
||||
if (!withitem_type) return 0;
|
||||
if (!add_attributes(withitem_type, NULL, 0)) return 0;
|
||||
initialized = 1;
|
||||
return 1;
|
||||
}
|
||||
|
@ -2213,9 +2219,8 @@ ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
|
|||
}
|
||||
|
||||
arguments_ty
|
||||
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)
|
||||
arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
|
||||
kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
|
||||
{
|
||||
arguments_ty p;
|
||||
p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
|
||||
|
@ -2223,12 +2228,10 @@ arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
|
|||
return NULL;
|
||||
p->args = args;
|
||||
p->vararg = vararg;
|
||||
p->varargannotation = varargannotation;
|
||||
p->kwonlyargs = kwonlyargs;
|
||||
p->kwarg = kwarg;
|
||||
p->kwargannotation = kwargannotation;
|
||||
p->defaults = defaults;
|
||||
p->kw_defaults = kw_defaults;
|
||||
p->kwarg = kwarg;
|
||||
p->defaults = defaults;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@ -3413,29 +3416,24 @@ ast2obj_arguments(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_identifier(o->vararg);
|
||||
value = ast2obj_arg(o->vararg);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_expr(o->varargannotation);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_varargannotation, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_identifier(o->kwarg);
|
||||
value = ast2obj_list(o->kw_defaults, ast2obj_expr);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
|
||||
if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_expr(o->kwargannotation);
|
||||
value = ast2obj_arg(o->kwarg);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_kwargannotation, value) == -1)
|
||||
if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_list(o->defaults, ast2obj_expr);
|
||||
|
@ -3443,11 +3441,6 @@ ast2obj_arguments(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_list(o->kw_defaults, ast2obj_expr);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
return result;
|
||||
failed:
|
||||
Py_XDECREF(value);
|
||||
|
@ -3477,6 +3470,16 @@ ast2obj_arg(void* _o)
|
|||
if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_int(o->lineno);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
value = ast2obj_int(o->col_offset);
|
||||
if (!value) goto failed;
|
||||
if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
|
||||
goto failed;
|
||||
Py_DECREF(value);
|
||||
return result;
|
||||
failed:
|
||||
Py_XDECREF(value);
|
||||
|
@ -3843,7 +3846,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_returns)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_returns) && _PyObject_GetAttrId(obj, &PyId_returns) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_returns);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -3934,7 +3937,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_starargs)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_starargs) && _PyObject_GetAttrId(obj, &PyId_starargs) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -3945,7 +3948,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
} else {
|
||||
starargs = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_kwargs)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_kwargs) && _PyObject_GetAttrId(obj, &PyId_kwargs) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -4018,7 +4021,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
if (isinstance) {
|
||||
expr_ty value;
|
||||
|
||||
if (_PyObject_HasAttrId(obj, &PyId_value)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_value) && _PyObject_GetAttrId(obj, &PyId_value) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_value);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -4476,7 +4479,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
expr_ty exc;
|
||||
expr_ty cause;
|
||||
|
||||
if (_PyObject_HasAttrId(obj, &PyId_exc)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_exc) && _PyObject_GetAttrId(obj, &PyId_exc) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_exc);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -4487,7 +4490,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
} else {
|
||||
exc = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_cause)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_cause) && _PyObject_GetAttrId(obj, &PyId_cause) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_cause);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -4637,7 +4640,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_msg)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_msg) && _PyObject_GetAttrId(obj, &PyId_msg) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_msg);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -4697,7 +4700,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
asdl_seq* names;
|
||||
int level;
|
||||
|
||||
if (_PyObject_HasAttrId(obj, &PyId_module)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_module) && _PyObject_GetAttrId(obj, &PyId_module) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_module);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -4733,7 +4736,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_level)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_level) && _PyObject_GetAttrId(obj, &PyId_level) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_level);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -5452,7 +5455,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
|
|||
if (isinstance) {
|
||||
expr_ty value;
|
||||
|
||||
if (_PyObject_HasAttrId(obj, &PyId_value)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_value) && _PyObject_GetAttrId(obj, &PyId_value) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_value);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -5639,7 +5642,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_starargs)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_starargs) && _PyObject_GetAttrId(obj, &PyId_starargs) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -5650,7 +5653,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
|
|||
} else {
|
||||
starargs = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_kwargs)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_kwargs) && _PyObject_GetAttrId(obj, &PyId_kwargs) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -6121,7 +6124,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
|
|||
expr_ty upper;
|
||||
expr_ty step;
|
||||
|
||||
if (_PyObject_HasAttrId(obj, &PyId_lower)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_lower) && _PyObject_GetAttrId(obj, &PyId_lower) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_lower);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -6132,7 +6135,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
|
|||
} else {
|
||||
lower = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_upper)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_upper) && _PyObject_GetAttrId(obj, &PyId_upper) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_upper);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -6143,7 +6146,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
|
|||
} else {
|
||||
upper = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_step)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_step) && _PyObject_GetAttrId(obj, &PyId_step) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_step);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -6598,7 +6601,7 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
|
|||
identifier name;
|
||||
asdl_seq* body;
|
||||
|
||||
if (_PyObject_HasAttrId(obj, &PyId_type)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_type) && _PyObject_GetAttrId(obj, &PyId_type) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_type);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -6609,7 +6612,7 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
|
|||
} else {
|
||||
type = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_name)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_name) && _PyObject_GetAttrId(obj, &PyId_name) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_name);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -6662,13 +6665,11 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
|
|||
{
|
||||
PyObject* tmp = NULL;
|
||||
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;
|
||||
|
||||
if (_PyObject_HasAttrId(obj, &PyId_args)) {
|
||||
int res;
|
||||
|
@ -6695,28 +6696,17 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_vararg)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_vararg) && _PyObject_GetAttrId(obj, &PyId_vararg) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
|
||||
if (tmp == NULL) goto failed;
|
||||
res = obj2ast_identifier(tmp, &vararg, arena);
|
||||
res = obj2ast_arg(tmp, &vararg, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_XDECREF(tmp);
|
||||
tmp = NULL;
|
||||
} else {
|
||||
vararg = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_varargannotation)) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_varargannotation);
|
||||
if (tmp == NULL) goto failed;
|
||||
res = obj2ast_expr(tmp, &varargannotation, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_XDECREF(tmp);
|
||||
tmp = NULL;
|
||||
} else {
|
||||
varargannotation = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
|
||||
int res;
|
||||
Py_ssize_t len;
|
||||
|
@ -6742,53 +6732,6 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_kwarg)) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
|
||||
if (tmp == NULL) goto failed;
|
||||
res = obj2ast_identifier(tmp, &kwarg, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_XDECREF(tmp);
|
||||
tmp = NULL;
|
||||
} else {
|
||||
kwarg = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_kwargannotation)) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_kwargannotation);
|
||||
if (tmp == NULL) goto failed;
|
||||
res = obj2ast_expr(tmp, &kwargannotation, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_XDECREF(tmp);
|
||||
tmp = NULL;
|
||||
} else {
|
||||
kwargannotation = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
|
||||
int res;
|
||||
Py_ssize_t len;
|
||||
Py_ssize_t i;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
|
||||
if (tmp == NULL) goto failed;
|
||||
if (!PyList_Check(tmp)) {
|
||||
PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
|
||||
goto failed;
|
||||
}
|
||||
len = PyList_GET_SIZE(tmp);
|
||||
defaults = asdl_seq_new(len, arena);
|
||||
if (defaults == NULL) goto failed;
|
||||
for (i = 0; i < len; i++) {
|
||||
expr_ty value;
|
||||
res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
|
||||
if (res != 0) goto failed;
|
||||
asdl_seq_SET(defaults, i, value);
|
||||
}
|
||||
Py_XDECREF(tmp);
|
||||
tmp = NULL;
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
|
||||
int res;
|
||||
Py_ssize_t len;
|
||||
|
@ -6814,8 +6757,44 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
|
||||
return 1;
|
||||
}
|
||||
*out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
|
||||
kwargannotation, defaults, kw_defaults, arena);
|
||||
if (_PyObject_HasAttrId(obj, &PyId_kwarg) && _PyObject_GetAttrId(obj, &PyId_kwarg) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
|
||||
if (tmp == NULL) goto failed;
|
||||
res = obj2ast_arg(tmp, &kwarg, arena);
|
||||
if (res != 0) goto failed;
|
||||
Py_XDECREF(tmp);
|
||||
tmp = NULL;
|
||||
} else {
|
||||
kwarg = NULL;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
|
||||
int res;
|
||||
Py_ssize_t len;
|
||||
Py_ssize_t i;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
|
||||
if (tmp == NULL) goto failed;
|
||||
if (!PyList_Check(tmp)) {
|
||||
PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
|
||||
goto failed;
|
||||
}
|
||||
len = PyList_GET_SIZE(tmp);
|
||||
defaults = asdl_seq_new(len, arena);
|
||||
if (defaults == NULL) goto failed;
|
||||
for (i = 0; i < len; i++) {
|
||||
expr_ty value;
|
||||
res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
|
||||
if (res != 0) goto failed;
|
||||
asdl_seq_SET(defaults, i, value);
|
||||
}
|
||||
Py_XDECREF(tmp);
|
||||
tmp = NULL;
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
|
||||
return 1;
|
||||
}
|
||||
*out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg,
|
||||
defaults, arena);
|
||||
return 0;
|
||||
failed:
|
||||
Py_XDECREF(tmp);
|
||||
|
@ -6841,7 +6820,7 @@ obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_annotation)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_annotation) && _PyObject_GetAttrId(obj, &PyId_annotation) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -6916,7 +6895,7 @@ obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_asname)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_asname) && _PyObject_GetAttrId(obj, &PyId_asname) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_asname);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
@ -6953,7 +6932,7 @@ obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
|
|||
PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
|
||||
return 1;
|
||||
}
|
||||
if (_PyObject_HasAttrId(obj, &PyId_optional_vars)) {
|
||||
if (_PyObject_HasAttrId(obj, &PyId_optional_vars) && _PyObject_GetAttrId(obj, &PyId_optional_vars) != Py_None) {
|
||||
int res;
|
||||
tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
|
||||
if (tmp == NULL) goto failed;
|
||||
|
|
72
Python/ast.c
72
Python/ast.c
|
@ -109,22 +109,14 @@ validate_arguments(arguments_ty args)
|
|||
{
|
||||
if (!validate_args(args->args))
|
||||
return 0;
|
||||
if (args->varargannotation) {
|
||||
if (!args->vararg) {
|
||||
PyErr_SetString(PyExc_ValueError, "varargannotation but no vararg on arguments");
|
||||
return 0;
|
||||
}
|
||||
if (!validate_expr(args->varargannotation, Load))
|
||||
if (args->vararg && args->vararg->annotation
|
||||
&& !validate_expr(args->vararg->annotation, Load)) {
|
||||
return 0;
|
||||
}
|
||||
if (!validate_args(args->kwonlyargs))
|
||||
return 0;
|
||||
if (args->kwargannotation) {
|
||||
if (!args->kwarg) {
|
||||
PyErr_SetString(PyExc_ValueError, "kwargannotation but no kwarg on arguments");
|
||||
return 0;
|
||||
}
|
||||
if (!validate_expr(args->kwargannotation, Load))
|
||||
if (args->kwarg && args->kwarg->annotation
|
||||
&& !validate_expr(args->kwarg->annotation, Load)) {
|
||||
return 0;
|
||||
}
|
||||
if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
|
||||
|
@ -1138,7 +1130,13 @@ ast_for_arg(struct compiling *c, const node *n)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
return arg(name, annotation, c->c_arena);
|
||||
arg_ty tmp = arg(name, annotation, c->c_arena);
|
||||
if (!tmp)
|
||||
return NULL;
|
||||
|
||||
tmp->lineno = LINENO(n);
|
||||
tmp->col_offset = n->n_col_offset;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/* returns -1 if failed to handle keyword only arguments
|
||||
|
@ -1234,15 +1232,13 @@ ast_for_arguments(struct compiling *c, const node *n)
|
|||
int i, j, k, nposargs = 0, nkwonlyargs = 0;
|
||||
int nposdefaults = 0, found_default = 0;
|
||||
asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
|
||||
identifier vararg = NULL, kwarg = NULL;
|
||||
arg_ty vararg = NULL, kwarg = NULL;
|
||||
arg_ty arg;
|
||||
expr_ty varargannotation = NULL, kwargannotation = NULL;
|
||||
node *ch;
|
||||
|
||||
if (TYPE(n) == parameters) {
|
||||
if (NCH(n) == 2) /* () as argument list */
|
||||
return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
||||
NULL, c->c_arena);
|
||||
return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
|
||||
n = CHILD(n, 1);
|
||||
}
|
||||
assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
|
||||
|
@ -1348,17 +1344,10 @@ ast_for_arguments(struct compiling *c, const node *n)
|
|||
i = res; /* res has new position to process */
|
||||
}
|
||||
else {
|
||||
vararg = NEW_IDENTIFIER(CHILD(ch, 0));
|
||||
vararg = ast_for_arg(c, ch);
|
||||
if (!vararg)
|
||||
return NULL;
|
||||
if (forbidden_name(c, vararg, CHILD(ch, 0), 0))
|
||||
return NULL;
|
||||
if (NCH(ch) > 1) {
|
||||
/* there is an annotation on the vararg */
|
||||
varargannotation = ast_for_expr(c, CHILD(ch, 2));
|
||||
if (!varargannotation)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
i += 3;
|
||||
if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
|
||||
|| TYPE(CHILD(n, i)) == vfpdef)) {
|
||||
|
@ -1373,17 +1362,9 @@ ast_for_arguments(struct compiling *c, const node *n)
|
|||
case DOUBLESTAR:
|
||||
ch = CHILD(n, i+1); /* tfpdef */
|
||||
assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
|
||||
kwarg = NEW_IDENTIFIER(CHILD(ch, 0));
|
||||
kwarg = ast_for_arg(c, ch);
|
||||
if (!kwarg)
|
||||
return NULL;
|
||||
if (NCH(ch) > 1) {
|
||||
/* there is an annotation on the kwarg */
|
||||
kwargannotation = ast_for_expr(c, CHILD(ch, 2));
|
||||
if (!kwargannotation)
|
||||
return NULL;
|
||||
}
|
||||
if (forbidden_name(c, kwarg, CHILD(ch, 0), 0))
|
||||
return NULL;
|
||||
i += 3;
|
||||
break;
|
||||
default:
|
||||
|
@ -1393,8 +1374,7 @@ ast_for_arguments(struct compiling *c, const node *n)
|
|||
return NULL;
|
||||
}
|
||||
}
|
||||
return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg,
|
||||
kwargannotation, posdefaults, kwdefaults, c->c_arena);
|
||||
return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
|
||||
}
|
||||
|
||||
static expr_ty
|
||||
|
@ -1559,8 +1539,7 @@ ast_for_lambdef(struct compiling *c, const node *n)
|
|||
expr_ty expression;
|
||||
|
||||
if (NCH(n) == 3) {
|
||||
args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
||||
NULL, c->c_arena);
|
||||
args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
|
||||
if (!args)
|
||||
return NULL;
|
||||
expression = ast_for_expr(c, CHILD(n, 2));
|
||||
|
@ -2107,15 +2086,22 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
|
|||
if (NCH(n) == 2)
|
||||
return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
|
||||
n->n_col_offset, c->c_arena);
|
||||
else
|
||||
return ast_for_call(c, CHILD(n, 1), left_expr);
|
||||
else {
|
||||
expr_ty tmp = ast_for_call(c, CHILD(n, 1), left_expr);
|
||||
if (!tmp)
|
||||
return NULL;
|
||||
|
||||
tmp->lineno = LINENO(n);
|
||||
tmp->col_offset = n->n_col_offset;
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
else if (TYPE(CHILD(n, 0)) == DOT ) {
|
||||
PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
|
||||
if (!attr_id)
|
||||
return NULL;
|
||||
return Attribute(left_expr, attr_id, Load,
|
||||
LINENO(n), n->n_col_offset, c->c_arena);
|
||||
LINENO(CHILD(n, 1)), CHILD(n, 1)->n_col_offset, c->c_arena);
|
||||
}
|
||||
else {
|
||||
REQ(CHILD(n, 0), LSQB);
|
||||
|
@ -2216,8 +2202,6 @@ ast_for_power(struct compiling *c, const node *n)
|
|||
tmp = ast_for_trailer(c, ch, e);
|
||||
if (!tmp)
|
||||
return NULL;
|
||||
tmp->lineno = e->lineno;
|
||||
tmp->col_offset = e->col_offset;
|
||||
e = tmp;
|
||||
}
|
||||
if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
|
||||
|
|
|
@ -1498,15 +1498,15 @@ compiler_visit_annotations(struct compiler *c, arguments_ty args,
|
|||
|
||||
if (compiler_visit_argannotations(c, args->args, names))
|
||||
goto error;
|
||||
if (args->varargannotation &&
|
||||
compiler_visit_argannotation(c, args->vararg,
|
||||
args->varargannotation, names))
|
||||
if (args->vararg && args->vararg->annotation &&
|
||||
compiler_visit_argannotation(c, args->vararg->arg,
|
||||
args->vararg->annotation, names))
|
||||
goto error;
|
||||
if (compiler_visit_argannotations(c, args->kwonlyargs, names))
|
||||
goto error;
|
||||
if (args->kwargannotation &&
|
||||
compiler_visit_argannotation(c, args->kwarg,
|
||||
args->kwargannotation, names))
|
||||
if (args->kwarg && args->kwarg->annotation &&
|
||||
compiler_visit_argannotation(c, args->kwarg->arg,
|
||||
args->kwarg->annotation, names))
|
||||
goto error;
|
||||
|
||||
if (!return_str) {
|
||||
|
|
|
@ -1530,10 +1530,10 @@ symtable_visit_annotations(struct symtable *st, stmt_ty s)
|
|||
|
||||
if (a->args && !symtable_visit_argannotations(st, a->args))
|
||||
return 0;
|
||||
if (a->varargannotation)
|
||||
VISIT(st, expr, a->varargannotation);
|
||||
if (a->kwargannotation)
|
||||
VISIT(st, expr, a->kwargannotation);
|
||||
if (a->vararg && a->vararg->annotation)
|
||||
VISIT(st, expr, a->vararg->annotation);
|
||||
if (a->kwarg && a->kwarg->annotation)
|
||||
VISIT(st, expr, a->kwarg->annotation);
|
||||
if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
|
||||
return 0;
|
||||
if (s->v.FunctionDef.returns)
|
||||
|
@ -1552,12 +1552,12 @@ symtable_visit_arguments(struct symtable *st, arguments_ty a)
|
|||
if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
|
||||
return 0;
|
||||
if (a->vararg) {
|
||||
if (!symtable_add_def(st, a->vararg, DEF_PARAM))
|
||||
if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
|
||||
return 0;
|
||||
st->st_cur->ste_varargs = 1;
|
||||
}
|
||||
if (a->kwarg) {
|
||||
if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
|
||||
if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
|
||||
return 0;
|
||||
st->st_cur->ste_varkeywords = 1;
|
||||
}
|
||||
|
|
|
@ -518,10 +518,10 @@ class Unparser:
|
|||
else: self.write(", ")
|
||||
self.write("*")
|
||||
if t.vararg:
|
||||
self.write(t.vararg)
|
||||
if t.varargannotation:
|
||||
self.write(t.vararg.arg)
|
||||
if t.vararg.annotation:
|
||||
self.write(": ")
|
||||
self.dispatch(t.varargannotation)
|
||||
self.dispatch(t.vararg.annotation)
|
||||
|
||||
# keyword-only arguments
|
||||
if t.kwonlyargs:
|
||||
|
@ -537,10 +537,10 @@ class Unparser:
|
|||
if t.kwarg:
|
||||
if first:first = False
|
||||
else: self.write(", ")
|
||||
self.write("**"+t.kwarg)
|
||||
if t.kwargannotation:
|
||||
self.write("**"+t.kwarg.arg)
|
||||
if t.kwarg.annotation:
|
||||
self.write(": ")
|
||||
self.dispatch(t.kwargannotation)
|
||||
self.dispatch(t.kwarg.annotation)
|
||||
|
||||
def _keyword(self, t):
|
||||
self.write(t.arg)
|
||||
|
|
Loading…
Reference in New Issue