From 24b4317bffeacbdda633d49e66e3ab5b4d2b4c07 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Fri, 22 May 2020 12:46:21 +0300 Subject: [PATCH 1/2] bpo-40726: handle uninitalized end_lineno on ast.increment_lineno --- Lib/ast.py | 7 +++++-- Lib/test/test_ast.py | 5 +++++ .../next/Library/2020-05-22-12-45-58.bpo-40726.7oBdMw.rst | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-05-22-12-45-58.bpo-40726.7oBdMw.rst diff --git a/Lib/ast.py b/Lib/ast.py index 52e51b48587747..3a778495f93531 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -229,8 +229,11 @@ def increment_lineno(node, n=1): for child in walk(node): if 'lineno' in child._attributes: child.lineno = getattr(child, 'lineno', 0) + n - if 'end_lineno' in child._attributes: - child.end_lineno = getattr(child, 'end_lineno', 0) + n + if ( + "end_lineno" in child._attributes + and (end_lineno := getattr(child, "end_lineno", 0)) is not None + ): + child.end_lineno = end_lineno + n return node diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index e55d10badc37e1..b9972e31941e14 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -830,6 +830,11 @@ def test_increment_lineno(self): 'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, ' 'col_offset=0, end_lineno=4, end_col_offset=5))' ) + src = ast.Call( + func=ast.Name("test", ast.Load()), args=[], keywords=[], lineno=1 + ) + self.assertEqual(ast.increment_lineno(src).lineno, 2) + self.assertIsNone(ast.increment_lineno(src).end_lineno) def test_iter_fields(self): node = ast.parse('foo()', mode='eval') diff --git a/Misc/NEWS.d/next/Library/2020-05-22-12-45-58.bpo-40726.7oBdMw.rst b/Misc/NEWS.d/next/Library/2020-05-22-12-45-58.bpo-40726.7oBdMw.rst new file mode 100644 index 00000000000000..7409eb3d80df64 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-22-12-45-58.bpo-40726.7oBdMw.rst @@ -0,0 +1,2 @@ +Handle cases where the ``end_lineno`` is ``None`` on +:func:`ast.increment_lineno`. From b80ce72e9efafa38dc2ff262522dfa27d073f0d1 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Fri, 22 May 2020 15:18:12 +0300 Subject: [PATCH 2/2] fix copy_location --- Lib/ast.py | 6 +++++- Lib/test/test_ast.py | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Lib/ast.py b/Lib/ast.py index 3a778495f93531..b4788263be102d 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -180,7 +180,11 @@ def copy_location(new_node, old_node): for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset': if attr in old_node._attributes and attr in new_node._attributes: value = getattr(old_node, attr, None) - if value is not None: + # end_lineno and end_col_offset are optional attributes, and they + # should be copied whether the value is None or not. + if value is not None or ( + hasattr(old_node, attr) and attr.startswith("end_") + ): setattr(new_node, attr, value) return new_node diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index b9972e31941e14..765a7ed74b4ae1 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -791,6 +791,12 @@ def test_copy_location(self): 'lineno=1, col_offset=4, end_lineno=1, end_col_offset=5), lineno=1, ' 'col_offset=0, end_lineno=1, end_col_offset=5))' ) + src = ast.Call(col_offset=1, lineno=1, end_lineno=1, end_col_offset=1) + new = ast.copy_location(src, ast.Call(col_offset=None, lineno=None)) + self.assertIsNone(new.end_lineno) + self.assertIsNone(new.end_col_offset) + self.assertEqual(new.lineno, 1) + self.assertEqual(new.col_offset, 1) def test_fix_missing_locations(self): src = ast.parse('write("spam")')