Skip to content

Commit 24b4317

Browse files
committed
bpo-40726: handle uninitalized end_lineno on ast.increment_lineno
1 parent b831129 commit 24b4317

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Lib/ast.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,11 @@ def increment_lineno(node, n=1):
229229
for child in walk(node):
230230
if 'lineno' in child._attributes:
231231
child.lineno = getattr(child, 'lineno', 0) + n
232-
if 'end_lineno' in child._attributes:
233-
child.end_lineno = getattr(child, 'end_lineno', 0) + n
232+
if (
233+
"end_lineno" in child._attributes
234+
and (end_lineno := getattr(child, "end_lineno", 0)) is not None
235+
):
236+
child.end_lineno = end_lineno + n
234237
return node
235238

236239

Lib/test/test_ast.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,11 @@ def test_increment_lineno(self):
830830
'lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, '
831831
'col_offset=0, end_lineno=4, end_col_offset=5))'
832832
)
833+
src = ast.Call(
834+
func=ast.Name("test", ast.Load()), args=[], keywords=[], lineno=1
835+
)
836+
self.assertEqual(ast.increment_lineno(src).lineno, 2)
837+
self.assertIsNone(ast.increment_lineno(src).end_lineno)
833838

834839
def test_iter_fields(self):
835840
node = ast.parse('foo()', mode='eval')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Handle cases where the ``end_lineno`` is ``None`` on
2+
:func:`ast.increment_lineno`.

0 commit comments

Comments
 (0)