summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-07-08 21:41:34 (GMT)
committerGitHub <noreply@github.com>2019-07-08 21:41:34 (GMT)
commitc7be35c2abd598f02a633879133caec356593241 (patch)
tree4a1e5b6a7de8e105ef4d2295e63e83b68f167f28 /Lib
parent54348f46f8c8c023b7e78f9cebe8e54818227b92 (diff)
downloadcpython-c7be35c2abd598f02a633879133caec356593241.zip
cpython-c7be35c2abd598f02a633879133caec356593241.tar.gz
cpython-c7be35c2abd598f02a633879133caec356593241.tar.bz2
bpo-18374: fix wrong col_offset of some ast.BinOp instances (GH-14607)
Nested BinOp instances (e.g. a+b+c) had a wrong col_offset for the second BinOp (e.g. 2 instead of 0 in the example). Fix it by using the correct st node to copy the line and col_offset from in ast.c. (cherry picked from commit 110a47c4f42cf4db88edc1876899fff8f05190fb) Co-authored-by: Carl Friedrich Bolz-Tereick <cfbolz@gmx.de>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_ast.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index e251e25..1e07c57 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -576,6 +576,36 @@ class AST_Tests(unittest.TestCase):
with support.swap_attr(unicodedata, 'normalize', bad_normalize):
self.assertRaises(TypeError, ast.parse, '\u03D5')
+ def test_issue18374_binop_col_offset(self):
+ tree = ast.parse('4+5+6+7')
+ parent_binop = tree.body[0].value
+ child_binop = parent_binop.left
+ grandchild_binop = child_binop.left
+ self.assertEqual(parent_binop.col_offset, 0)
+ self.assertEqual(parent_binop.end_col_offset, 7)
+ self.assertEqual(child_binop.col_offset, 0)
+ self.assertEqual(child_binop.end_col_offset, 5)
+ self.assertEqual(grandchild_binop.col_offset, 0)
+ self.assertEqual(grandchild_binop.end_col_offset, 3)
+
+ tree = ast.parse('4+5-\\\n 6-7')
+ parent_binop = tree.body[0].value
+ child_binop = parent_binop.left
+ grandchild_binop = child_binop.left
+ self.assertEqual(parent_binop.col_offset, 0)
+ self.assertEqual(parent_binop.lineno, 1)
+ self.assertEqual(parent_binop.end_col_offset, 4)
+ self.assertEqual(parent_binop.end_lineno, 2)
+
+ self.assertEqual(child_binop.col_offset, 0)
+ self.assertEqual(parent_binop.lineno, 1)
+ self.assertEqual(child_binop.end_col_offset, 2)
+ self.assertEqual(parent_binop.end_lineno, 2)
+
+ self.assertEqual(grandchild_binop.col_offset, 0)
+ self.assertEqual(parent_binop.lineno, 1)
+ self.assertEqual(grandchild_binop.end_col_offset, 3)
+ self.assertEqual(parent_binop.end_lineno, 2)
class ASTHelpers_Test(unittest.TestCase):
maxDiff = None