[Python-checkins] [3.7] bpo-39031: Include elif keyword when producing lineno/col-offset info for if_stmt (GH-17582) (#17584)

Pablo Galindo webhook-mailer at python.org
Fri Dec 13 09:04:19 EST 2019


https://github.com/python/cpython/commit/0ed45d0cbfc7579dfc5527c19aa6e4bb696db2e0
commit: 0ed45d0cbfc7579dfc5527c19aa6e4bb696db2e0
branch: 3.7
author: Pablo Galindo <Pablogsal at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019年12月13日T14:04:14Z
summary:
[3.7] bpo-39031: Include elif keyword when producing lineno/col-offset info for if_stmt (GH-17582) (#17584)
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 025a602af7ee284d8db6955c26016f3f27d35536)
Co-authored-by: Lysandros Nikolaou <lisandrosnik at gmail.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2019-12-12-21-05-43.bpo-39031.imlCYZ.rst
M Lib/test/test_ast.py
M Python/ast.c
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 2d2fb26baa9c0..0daa2f77dc718 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -64,6 +64,8 @@ def to_tuple(t):
 "while v:pass",
 # If
 "if v:pass",
+ # If-Elif
+ "if a:\n pass\nelif b:\n pass",
 # With
 "with x as y: pass",
 "with x as y, z as q: pass",
@@ -598,6 +600,12 @@ def test_get_docstring_none(self):
 node = ast.parse('async def foo():\n x = "not docstring"')
 self.assertIsNone(ast.get_docstring(node.body[0]))
 
+ def test_elif_stmt_start_position(self):
+ node = ast.parse('if a:\n pass\nelif b:\n pass\n')
+ elif_stmt = node.body[0].orelse[0]
+ self.assertEqual(elif_stmt.lineno, 3)
+ self.assertEqual(elif_stmt.col_offset, 0)
+
 def test_literal_eval(self):
 self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
 self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
@@ -1235,6 +1243,7 @@ def main():
 ('Module', [('For', (1, 0), ('Name', (1, 4), 'v', ('Store',)), ('Name', (1, 9), 'v', ('Load',)), [('Pass', (1, 11))], [])]),
 ('Module', [('While', (1, 0), ('Name', (1, 6), 'v', ('Load',)), [('Pass', (1, 8))], [])]),
 ('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
+('Module', [('If', (1, 0), ('Name', (1, 3), 'a', ('Load',)), [('Pass', (2, 2))], [('If', (3, 0), ('Name', (3, 5), 'b', ('Load',)), [('Pass', (4, 2))], [])])]),
 ('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)]),
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-12-12-21-05-43.bpo-39031.imlCYZ.rst b/Misc/NEWS.d/next/Core and Builtins/2019-12-12-21-05-43.bpo-39031.imlCYZ.rst
new file mode 100644
index 0000000000000..738902ce907ad
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-12-12-21-05-43.bpo-39031.imlCYZ.rst	
@@ -0,0 +1,2 @@
+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.
+Patch by Lysandros Nikolaou.
diff --git a/Python/ast.c b/Python/ast.c
index 5a60d6994d6a6..d1b87d888e5c2 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -3622,8 +3622,8 @@ ast_for_if_stmt(struct compiling *c, const node *n)
 
 asdl_seq_SET(newobj, 0,
 If(expression, suite_seq, orelse,
- LINENO(CHILD(n, off)),
- CHILD(n, off)->n_col_offset, c->c_arena));
+ LINENO(CHILD(n, off - 1)),
+ CHILD(n, off - 1)->n_col_offset, c->c_arena));
 orelse = newobj;
 }
 expression = ast_for_expr(c, CHILD(n, 1));


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /