Skip to content

Commit b80ce72

Browse files
committed
fix copy_location
1 parent 24b4317 commit b80ce72

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Lib/ast.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ def copy_location(new_node, old_node):
180180
for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset':
181181
if attr in old_node._attributes and attr in new_node._attributes:
182182
value = getattr(old_node, attr, None)
183-
if value is not None:
183+
# end_lineno and end_col_offset are optional attributes, and they
184+
# should be copied whether the value is None or not.
185+
if value is not None or (
186+
hasattr(old_node, attr) and attr.startswith("end_")
187+
):
184188
setattr(new_node, attr, value)
185189
return new_node
186190

Lib/test/test_ast.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,12 @@ def test_copy_location(self):
791791
'lineno=1, col_offset=4, end_lineno=1, end_col_offset=5), lineno=1, '
792792
'col_offset=0, end_lineno=1, end_col_offset=5))'
793793
)
794+
src = ast.Call(col_offset=1, lineno=1, end_lineno=1, end_col_offset=1)
795+
new = ast.copy_location(src, ast.Call(col_offset=None, lineno=None))
796+
self.assertIsNone(new.end_lineno)
797+
self.assertIsNone(new.end_col_offset)
798+
self.assertEqual(new.lineno, 1)
799+
self.assertEqual(new.col_offset, 1)
794800

795801
def test_fix_missing_locations(self):
796802
src = ast.parse('write("spam")')

0 commit comments

Comments
 (0)