[Python-checkins] [3.8] bpo-39579: Fix Attribute end_col_offset to point at the current node (GH-18405) (GH-18408)

Lysandros Nikolaou webhook-mailer at python.org
Fri Feb 7 19:21:48 EST 2020


https://github.com/python/cpython/commit/8b9cebce09cb6919fdb97d8e608288a503681d13
commit: 8b9cebce09cb6919fdb97d8e608288a503681d13
branch: 3.8
author: Lysandros Nikolaou <lisandrosnik at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-02-07T16:21:38-08:00
summary:

[3.8] bpo-39579: Fix Attribute end_col_offset to point at the current node (GH-18405) (GH-18408)



(cherry picked from commit d2e1098641f98594702ef29049c3c4a3f394786f)





https://bugs.python.org/issue39579



Automerge-Triggered-By: @gvanrossum

files:
A Misc/NEWS.d/next/Core and Builtins/2020-02-07-15-18-35.bpo-39579.itNmC0.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 69ed83cd8b68b..e843d53781d25 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -140,6 +140,8 @@ def to_tuple(t):
     "@deco1\n at deco2()\n at deco3(1)\nclass C: pass",
     # Decorator with generator argument
     "@deco(a for a in b)\ndef f(): pass",
+    # Decorator with attribute
+    "@a.b.c\ndef f(): pass",
     # Simple assignment expression
     "(a := 1)",
     # Positional-only arguments
@@ -614,6 +616,11 @@ def test_issue18374_binop_col_offset(self):
         self.assertEqual(grandchild_binop.end_col_offset, 3)
         self.assertEqual(grandchild_binop.end_lineno, 1)
 
+    def test_issue39579_dotted_name_end_col_offset(self):
+        tree = ast.parse('@a.b.c\ndef f(): pass')
+        attr_b = tree.body[0].decorator_list[0].value
+        self.assertEqual(attr_b.end_col_offset, 4)
+
 class ASTHelpers_Test(unittest.TestCase):
     maxDiff = None
 
@@ -1838,6 +1845,7 @@ def main():
 ('Module', [('AsyncFunctionDef', (4, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (4, 15))], [('Name', (1, 1), 'deco1', ('Load',)), ('Call', (2, 1), ('Name', (2, 1), 'deco2', ('Load',)), [], []), ('Call', (3, 1), ('Name', (3, 1), 'deco3', ('Load',)), [('Constant', (3, 7), 1, None)], [])], None, None)], []),
 ('Module', [('ClassDef', (4, 0), 'C', [], [], [('Pass', (4, 9))], [('Name', (1, 1), 'deco1', ('Load',)), ('Call', (2, 1), ('Name', (2, 1), 'deco2', ('Load',)), [], []), ('Call', (3, 1), ('Name', (3, 1), 'deco3', ('Load',)), [('Constant', (3, 7), 1, None)], [])])], []),
 ('Module', [('FunctionDef', (2, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (2, 9))], [('Call', (1, 1), ('Name', (1, 1), 'deco', ('Load',)), [('GeneratorExp', (1, 5), ('Name', (1, 6), 'a', ('Load',)), [('comprehension', ('Name', (1, 12), 'a', ('Store',)), ('Name', (1, 17), 'b', ('Load',)), [], 0)])], [])], None, None)], []),
+('Module', [('FunctionDef', (2, 0), 'f', ('arguments', [], [], None, [], [], None, []), [('Pass', (2, 9))], [('Attribute', (1, 1), ('Attribute', (1, 1), ('Name', (1, 1), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',))], None, None)], []),
 ('Module', [('Expr', (1, 0), ('NamedExpr', (1, 1), ('Name', (1, 1), 'a', ('Store',)), ('Constant', (1, 6), 1, None)))], []),
 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None, None)], [], None, [], [], None, []), [('Pass', (1, 14))], [], None, None)], []),
 ('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None, None)], [('arg', (1, 12), 'c', None, None), ('arg', (1, 15), 'd', None, None), ('arg', (1, 18), 'e', None, None)], None, [], [], None, []), [('Pass', (1, 22))], [], None, None)], []),
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-02-07-15-18-35.bpo-39579.itNmC0.rst b/Misc/NEWS.d/next/Core and Builtins/2020-02-07-15-18-35.bpo-39579.itNmC0.rst
new file mode 100644
index 0000000000000..36d5c425670c2
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-02-07-15-18-35.bpo-39579.itNmC0.rst	
@@ -0,0 +1 @@
+Change the ending column offset of `Attribute` nodes constructed in `ast_for_dotted_name` to point at the end of the current node and not at the end of the last `NAME` node.
\ No newline at end of file
diff --git a/Python/ast.c b/Python/ast.c
index f3263c1e3fcdc..12f24f2c22ab9 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -1715,11 +1715,12 @@ ast_for_dotted_name(struct compiling *c, const node *n)
         return NULL;
 
     for (i = 2; i < NCH(n); i+=2) {
-        id = NEW_IDENTIFIER(CHILD(n, i));
+        const node *child = CHILD(n, i);
+        id = NEW_IDENTIFIER(child);
         if (!id)
             return NULL;
         e = Attribute(e, id, Load, lineno, col_offset,
-                      n->n_end_lineno, n->n_end_col_offset, c->c_arena);
+                      child->n_end_lineno, child->n_end_col_offset, c->c_arena);
         if (!e)
             return NULL;
     }



More information about the Python-checkins mailing list