Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 025a602

Browse files
lysnikolaoumiss-islington
authored andcommitted
bpo-39031: Include elif keyword when producing lineno/col-offset info for if_stmt (GH-17582)
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
1 parent 1988344 commit 025a602

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",
@@ -861,6 +863,12 @@ def test_multi_line_docstring_col_offset_and_lineno_issue16806(self):
861863
self.assertEqual(node.body[2].col_offset, 0)
862864
self.assertEqual(node.body[2].lineno, 13)
863865

866+
def test_elif_stmt_start_position(self):
867+
node = ast.parse('if a:\n pass\nelif b:\n pass\n')
868+
elif_stmt = node.body[0].orelse[0]
869+
self.assertEqual(elif_stmt.lineno, 3)
870+
self.assertEqual(elif_stmt.col_offset, 0)
871+
864872
def test_literal_eval(self):
865873
self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
866874
self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
@@ -1843,6 +1851,7 @@ def main():
18431851
('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [], None)], []),
18441852
('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])], []),
18451853
('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])], []),
1854+
('Module', [('If', (1, 0), ('Name', (1, 3), 'a', ('Load',)), [('Pass', (2, 2))], [('If', (3, 0), ('Name', (3, 5), 'b', ('Load',)), [('Pass', (4, 2))], [])])], []),
18461855
('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))], None)], []),
18471856
('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)], []),
18481857
('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
@@ -4076,8 +4076,8 @@ ast_for_if_stmt(struct compiling *c, const node *n)
40764076
}
40774077
asdl_seq_SET(newobj, 0,
40784078
If(expression, suite_seq, orelse,
4079-
LINENO(CHILD(n, off)),
4080-
CHILD(n, off)->n_col_offset,
4079+
LINENO(CHILD(n, off - 1)),
4080+
CHILD(n, off - 1)->n_col_offset,
40814081
end_lineno, end_col_offset, c->c_arena));
40824082
orelse = newobj;
40834083
}

0 commit comments

Comments
 (0)