bpo-40662: Fixed ast.get_source_segment for ast nodes that have incomplete location information (GH-20157)
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
This commit is contained in:
parent
63b8e0cba3
commit
e6578a226d
|
@ -332,6 +332,8 @@ def get_source_segment(source, node, *, padded=False):
|
|||
be padded with spaces to match its original position.
|
||||
"""
|
||||
try:
|
||||
if node.end_lineno is None or node.end_col_offset is None:
|
||||
return None
|
||||
lineno = node.lineno - 1
|
||||
end_lineno = node.end_lineno - 1
|
||||
col_offset = node.col_offset
|
||||
|
|
|
@ -1851,6 +1851,17 @@ class EndPositionTests(unittest.TestCase):
|
|||
cdef = ast.parse(s).body[0]
|
||||
self.assertEqual(ast.get_source_segment(s, cdef.body[0], padded=True), s_method)
|
||||
|
||||
def test_source_segment_missing_info(self):
|
||||
s = 'v = 1\r\nw = 1\nx = 1\n\ry = 1\r\n'
|
||||
v, w, x, y = ast.parse(s).body
|
||||
del v.lineno
|
||||
del w.end_lineno
|
||||
del x.col_offset
|
||||
del y.end_col_offset
|
||||
self.assertIsNone(ast.get_source_segment(s, v))
|
||||
self.assertIsNone(ast.get_source_segment(s, w))
|
||||
self.assertIsNone(ast.get_source_segment(s, x))
|
||||
self.assertIsNone(ast.get_source_segment(s, y))
|
||||
|
||||
class NodeVisitorTests(unittest.TestCase):
|
||||
def test_old_constant_nodes(self):
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Fixed :func:`ast.get_source_segment` for ast nodes that have incomplete location information. Patch by Irit Katriel.
|
Loading…
Reference in New Issue