8000 bpo-39031: Include elif keyword when producing lineno/col-offset info… · python/cpython@3b18b17 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b18b17

Browse files
miss-islingtonlysnikolaou
authored andcommitted
bpo-39031: Include elif keyword when producing lineno/col-offset info for if_stmt (GH-17582) (GH-17589)
When parsing an "elif" node, lineno and col_offset of the node now point to the "elif" keyword and not to its condition, making it consistent with the "if" node. https://bugs.python.org/issue39031 Automerge-Triggered-By: @pablogsal (cherry picked from commit 025a602) Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
1 parent b738237 commit 3b18b17

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

Lib/test/test_ast.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def to_tuple(t):
6868
"while v:pass",
6969
# If
7070
"if v:pass",
71+
# If-Elif
72+
"if a:\n pass\nelif b:\n pass",
7173
# With
7274
"with x as y: pass",
7375
"with x as y, z as q: pass",
@@ -799,6 +801,12 @@ def test_multi_line_docstring_col_offset_and_lineno_issue16806(self):
799801
self.assertEqual(node.body[2].col_offset, 0)
800802
self.assertEqual(node.body[2].lineno, 13)
801803

804+
def test_elif_stmt_start_position(self):
805+
node = ast.parse('if a:\n pass\nelif b:\n pass\n')
806+
elif_stmt = node.body[0].orelse[0]
807+
self.assertEqual(elif_stmt.lineno, 3)
808+
self.assertEqual(elif_stmt.col_offset, 0)
809+
802810
def test_literal_eval(self):
803811
self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
804812
self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
@@ -1781,6 +1789,7 @@ def main():
17811789
('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [], None)], []),
17821790
('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])], []),
17831791
('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])], []),
1792+
('Module', [('If', (1, 0), ('Name', (1, 3), 'a', ('Load',)), [('Pass', (2, 2))], [('If', (3, 0), ('Name', (3, 5), 'b', ('Load',)), [('Pass', (4, 2))], [])])], []),
17841793
('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))], None)], []),
17851794
('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))], None)], []),
17861795
('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Constant', (1, 16), 'string', None)], []), None)], []),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When parsing an "elif" node, lineno and col_offset of the node now point to the "elif" keyword and not to its condition, making it consistent with the "if" node.
2+
Patch by Lysandros Nikolaou.

Python/ast.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4079,8 +4079,8 @@ ast_for_if_stmt(struct compiling *c, const node *n)
40794079
}
40804080
asdl_seq_SET(newobj, 0,
40814081
If(expression, suite_seq, orelse,
4082-
LINENO(CHILD(n, off)),
4083-
CHILD(n, off)->n_col_offset,
4082+
LINENO(CHILD(n, off - 1)),
4083+
CHILD(n, off - 1)->n_col_offset,
40844084
end_lineno, end_col_offset, c->c_arena));
40854085
orelse = newobj;
40864086
}

0 commit comments

Comments
 (0)
0