Skip to content

Commit 90fc898

Browse files
guocimiss-islington
authored andcommitted
closes bpo-31902: Fix the col_offset attribute for ast.Async* nodes to point to the "async" keyword. (GH-4175)
Previously, col_offset points to the keyword after "async".
1 parent 9c22379 commit 90fc898

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

Lib/test/test_ast.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,12 +1181,12 @@ def main():
11811181
('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)]))]),
11821182
('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)]))]),
11831183
('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)]))]),
1184-
('Module', [('AsyncFunctionDef', (1, 6), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Str', (2, 1), 'async function')), ('Expr', (3, 1), ('Await', (3, 1), ('Call', (3, 7), ('Name', (3, 7), 'something', ('Load',)), [], [])))], [], None)]),
1185-
('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)]),
1186-
('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)]),
1184+
('Module', [('AsyncFunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Expr', (2, 1), ('Str', (2, 1), 'async function')), ('Expr', (3, 1), ('Await', (3, 1), ('Call', (3, 7), ('Name', (3, 7), 'something', ('Load',)), [], [])))], [], None)]),
1185+
('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)]),
1186+
('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)]),
11871187
('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)]))]),
11881188
('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)]))]),
1189-
('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)]),
1189+
('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)]),
11901190
]
11911191
single_results = [
11921192
('Interactive', [('Expr', (1, 0), ('BinOp', (1, 0), ('Num', (1, 0), 1), ('Add',), ('Num', (1, 2), 2)))]),
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the ``col_offset`` attribute for ast nodes ``ast.AsyncFor``,
2+
``ast.AsyncFunctionDef``, and ``ast.AsyncWith``. Previously, ``col_offset``
3+
pointed to the keyword after ``async``.

Python/ast.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,8 @@ static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
600600
static expr_ty ast_for_testlist(struct compiling *, const node *);
601601
static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
602602

603-
static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
604-
static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
603+
static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
604+
static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
605605

606606
/* Note different signature for ast_for_call */
607607
static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, bool);
@@ -1569,10 +1569,11 @@ ast_for_decorators(struct compiling *c, const node *n)
15691569
}
15701570

15711571
static stmt_ty
1572-
ast_for_funcdef_impl(struct compiling *c, const node *n,
1573-
asdl_seq *decorator_seq, int is_async)
1572+
ast_for_funcdef_impl(struct compiling *c, const node *n0,
1573+
asdl_seq *decorator_seq, bool is_async)
15741574
{
15751575
/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1576+
const node * const n = is_async ? CHILD(n0, 1) : n0;
15761577
identifier name;
15771578
arguments_ty args;
15781579
asdl_seq *body;
@@ -1601,7 +1602,7 @@ ast_for_funcdef_impl(struct compiling *c, const node *n,
16011602

16021603
if (is_async)
16031604
return AsyncFunctionDef(name, args, body, decorator_seq, returns,
1604-
LINENO(n), n->n_col_offset, c->c_arena);
1605+
LINENO(n), n0->n_col_offset, c->c_arena);
16051606
else
16061607
return FunctionDef(name, args, body, decorator_seq, returns,
16071608
LINENO(n), n->n_col_offset, c->c_arena);
@@ -1616,16 +1617,16 @@ ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_se
16161617
assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
16171618
REQ(CHILD(n, 1), funcdef);
16181619

1619-
return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1620-
1 /* is_async */);
1620+
return ast_for_funcdef_impl(c, n, decorator_seq,
1621+
true /* is_async */);
16211622
}
16221623

16231624
static stmt_ty
16241625
ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
16251626
{
16261627
/* funcdef: 'def' NAME parameters ['->' test] ':' suite */
16271628
return ast_for_funcdef_impl(c, n, decorator_seq,
1628-
0 /* is_async */);
1629+
false /* is_async */);
16291630
}
16301631

16311632

@@ -1639,15 +1640,15 @@ ast_for_async_stmt(struct compiling *c, const node *n)
16391640

16401641
switch (TYPE(CHILD(n, 1))) {
16411642
case funcdef:
1642-
return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1643-
1 /* is_async */);
1643+
return ast_for_funcdef_impl(c, n, NULL,
1644+
true /* is_async */);
16441645
case with_stmt:
1645-
return ast_for_with_stmt(c, CHILD(n, 1),
1646-
1 /* is_async */);
1646+
return ast_for_with_stmt(c, n,
1647+
true /* is_async */);
16471648

16481649
case for_stmt:
1649-
return ast_for_for_stmt(c, CHILD(n, 1),
1650-
1 /* is_async */);
1650+
return ast_for_for_stmt(c, n,
1651+
true /* is_async */);
16511652

16521653
default:
16531654
PyErr_Format(PyExc_SystemError,
@@ -3681,8 +3682,9 @@ ast_for_while_stmt(struct compiling *c, const node *n)
36813682
}
36823683

36833684
static stmt_ty
3684-
ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
3685+
ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
36853686
{
3687+
const node * const n = is_async ? CHILD(n0, 1) : n0;
36863688
asdl_seq *_target, *seq = NULL, *suite_seq;
36873689
expr_ty expression;
36883690
expr_ty target, first;
@@ -3717,7 +3719,7 @@ ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
37173719

37183720
if (is_async)
37193721
return AsyncFor(target, expression, suite_seq, seq,
3720-
LINENO(n), n->n_col_offset,
3722+
LINENO(n), n0->n_col_offset,
37213723
c->c_arena);
37223724
else
37233725
return For(target, expression, suite_seq, seq,
@@ -3869,8 +3871,9 @@ ast_for_with_item(struct compiling *c, const node *n)
38693871

38703872
/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
38713873
static stmt_ty
3872-
ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
3874+
ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
38733875
{
3876+
const node * const n = is_async ? CHILD(n0, 1) : n0;
38743877
int i, n_items;
38753878
asdl_seq *items, *body;
38763879

@@ -3892,7 +3895,7 @@ ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
38923895
return NULL;
38933896

38943897
if (is_async)
3895-
return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3898+
return AsyncWith(items, body, LINENO(n), n0->n_col_offset, c->c_arena);
38963899
else
38973900
return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
38983901
}

0 commit comments

Comments
 (0)