diff -r f889305d89a2 Lib/test/test_ast.py --- a/Lib/test/test_ast.py Sun Jan 08 00:37:32 2017 -0800 +++ b/Lib/test/test_ast.py Sun Jan 08 06:03:07 2017 -0800 @@ -109,6 +109,7 @@ "{r for l,m in x}", # AsyncFunctionDef "async def f():\n await something()", + "@decorator\nasync def f():\n await something()", # AsyncFor "async def f():\n async for e in i: 1\n else: 2", # AsyncWith @@ -1121,12 +1122,13 @@ ('Module', [('Expr', (1, 0), ('DictComp', (1, 0), ('Name', (1, 1), 'a', ('Load',)), ('Name', (1, 5), 'b', ('Load',)), [('comprehension', ('Tuple', (1, 11), [('Name', (1, 11), 'v', ('Store',)), ('Name', (1, 13), 'w', ('Store',))], ('Store',)), ('Name', (1, 18), 'x', ('Load',)), [], 0)]))]), ('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 12), 'x', ('Load',)), [('Name', (1, 17), 'g', ('Load',))], 0)]))]), ('Module', [('Expr', (1, 0), ('SetComp', (1, 0), ('Name', (1, 1), 'r', ('Load',)), [('comprehension', ('Tuple', (1, 7), [('Name', (1, 7), 'l', ('Store',)), ('Name', (1, 9), 'm', ('Store',))], ('Store',)), ('Name', (1, 14), 'x', ('Load',)), [], 0)]))]), -('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Await', (2, 1), ('Call', (2, 7), ('Name', (2, 7), 'something', ('Load',)), [], [])))], [], None)]), -('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncFor', (2, 7), ('Name', (2, 11), 'e', ('Store',)), ('Name', (2, 16), 'i', ('Load',)), [('Expr', (2, 19), ('Num', (2, 19), 1))], [('Expr', (3, 7), ('Num', (3, 7), 2))])], [], None)]), -('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('AsyncWith', (2, 7), [('withitem', ('Name', (2, 12), 'a', ('Load',)), ('Name', (2, 17), 'b', ('Store',)))], [('Expr', (2, 20), ('Num', (2, 20), 1))])], [], None)]), +('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Await', (2, 1), ('Call', (2, 7), ('Name', (2, 7), 'something', ('Load',)), [], [])))], [], None)]), +('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (3, 1), ('Await', (3, 1), ('Call', (3, 7), ('Name', (3, 7), 'something', ('Load',)), [], [])))], [('Name', (1, 1), 'decorator', ('Load',))], None)]), +('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('AsyncFor', (2, 1), ('Name', (2, 11), 'e', ('Store',)), ('Name', (2, 16), 'i', ('Load',)), [('Expr', (2, 19), ('Num', (2, 19), 1))], [('Expr', (3, 7), ('Num', (3, 7), 2))])], [], None)]), +('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('AsyncWith', (2, 1), [('withitem', ('Name', (2, 12), 'a', ('Load',)), ('Name', (2, 17), 'b', ('Store',)))], [('Expr', (2, 20), ('Num', (2, 20), 1))])], [], None)]), ('Module', [('Expr', (1, 0), ('Dict', (1, 0), [None, ('Num', (1, 10), 2)], [('Dict', (1, 3), [('Num', (1, 4), 1)], [('Num', (1, 6), 2)]), ('Num', (1, 12), 3)]))]), ('Module', [('Expr', (1, 0), ('Set', (1, 0), [('Starred', (1, 1), ('Set', (1, 2), [('Num', (1, 3), 1), ('Num', (1, 6), 2)]), ('Load',)), ('Num', (1, 10), 3)]))]), -('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('ListComp', (2, 2), ('Name', (2, 2), 'i', ('Load',)), [('comprehension', ('Name', (2, 14), 'b', ('Store',)), ('Name', (2, 19), 'c', ('Load',)), [], 1)]))], [], None)]), +('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('ListComp', (2, 2), ('Name', (2, 2), 'i', ('Load',)), [('comprehension', ('Name', (2, 14), 'b', ('Store',)), ('Name', (2, 19), 'c', ('Load',)), [], 1)]))], [], None)]), ] single_results = [ ('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]), diff -r f889305d89a2 Python/ast.c --- a/Python/ast.c Sun Jan 08 00:37:32 2017 -0800 +++ b/Python/ast.c Sun Jan 08 06:03:07 2017 -0800 @@ -600,8 +600,8 @@ static expr_ty ast_for_testlist(struct compiling *, const node *); static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); -static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int); -static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int); +static stmt_ty ast_for_with_stmt(struct compiling *, const node *, const node *); +static stmt_ty ast_for_for_stmt(struct compiling *, const node *, const node *); /* Note different signature for ast_for_call */ static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); @@ -1586,7 +1586,7 @@ static stmt_ty ast_for_funcdef_impl(struct compiling *c, const node *n, - asdl_seq *decorator_seq, int is_async) + asdl_seq *decorator_seq, const node *parent_n) { /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ identifier name; @@ -1615,10 +1615,10 @@ if (!body) return NULL; - if (is_async) + if (parent_n != NULL) return AsyncFunctionDef(name, args, body, decorator_seq, returns, - LINENO(n), - n->n_col_offset, c->c_arena); + LINENO(parent_n), + parent_n->n_col_offset, c->c_arena); else return FunctionDef(name, args, body, decorator_seq, returns, LINENO(n), @@ -1633,16 +1633,17 @@ REQ(CHILD(n, 0), ASYNC); REQ(CHILD(n, 1), funcdef); - return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq, - 1 /* is_async */); + /* pass the parent node explicitly so that the col_offset is correct + * (issue 29205) + */ + return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq, n); } static stmt_ty ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) { /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ - return ast_for_funcdef_impl(c, n, decorator_seq, - 0 /* is_async */); + return ast_for_funcdef_impl(c, n, decorator_seq, NULL); } @@ -1655,15 +1656,12 @@ switch (TYPE(CHILD(n, 1))) { case funcdef: - return ast_for_funcdef_impl(c, CHILD(n, 1), NULL, - 1 /* is_async */); + return ast_for_funcdef_impl(c, CHILD(n, 1), NULL, n); case with_stmt: - return ast_for_with_stmt(c, CHILD(n, 1), - 1 /* is_async */); + return ast_for_with_stmt(c, CHILD(n, 1), n); case for_stmt: - return ast_for_for_stmt(c, CHILD(n, 1), - 1 /* is_async */); + return ast_for_for_stmt(c, CHILD(n, 1), n); default: PyErr_Format(PyExc_SystemError, @@ -3676,7 +3674,7 @@ } static stmt_ty -ast_for_for_stmt(struct compiling *c, const node *n, int is_async) +ast_for_for_stmt(struct compiling *c, const node *n, const node *parent_n) { asdl_seq *_target, *seq = NULL, *suite_seq; expr_ty expression; @@ -3710,9 +3708,9 @@ if (!suite_seq) return NULL; - if (is_async) + if (parent_n != NULL) return AsyncFor(target, expression, suite_seq, seq, - LINENO(n), n->n_col_offset, + LINENO(parent_n), parent_n->n_col_offset, c->c_arena); else return For(target, expression, suite_seq, seq, @@ -3864,7 +3862,7 @@ /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ static stmt_ty -ast_for_with_stmt(struct compiling *c, const node *n, int is_async) +ast_for_with_stmt(struct compiling *c, const node *n, const node *parent_n) { int i, n_items; asdl_seq *items, *body; @@ -3886,8 +3884,9 @@ if (!body) return NULL; - if (is_async) - return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena); + if (parent_n != NULL) + return AsyncWith(items, body, LINENO(parent_n), + parent_n->n_col_offset, c->c_arena); else return With(items, body, LINENO(n), n->n_col_offset, c->c_arena); } @@ -4006,11 +4005,11 @@ case while_stmt: return ast_for_while_stmt(c, ch); case for_stmt: - return ast_for_for_stmt(c, ch, 0); + return ast_for_for_stmt(c, ch, NULL); case try_stmt: return ast_for_try_stmt(c, ch); case with_stmt: - return ast_for_with_stmt(c, ch, 0); + return ast_for_with_stmt(c, ch, NULL); case funcdef: return ast_for_funcdef(c, ch, NULL); case classdef: