MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / get_source_segment

Function get_source_segment

tools/python-3.11.9-amd64/Lib/ast.py:343–377  ·  view source on GitHub ↗

Get source code segment of the *source* that generated *node*. If some location information (`lineno`, `end_lineno`, `col_offset`, or `end_col_offset`) is missing, return None. If *padded* is `True`, the first line of a multi-line statement will be padded with spaces to match

(source, node, *, padded=False)

Source from the content-addressed store, hash-verified

341
342
343def get_source_segment(source, node, *, padded=False):
344 """Get source code segment of the *source* that generated *node*.
345
346 If some location information (`lineno`, `end_lineno`, `col_offset`,
347 or `end_col_offset`) is missing, return None.
348
349 If *padded* is `True`, the first line of a multi-line statement will
350 be padded with spaces to match its original position.
351 """
352 try:
353 if node.end_lineno is None or node.end_col_offset is None:
354 return None
355 lineno = node.lineno - 1
356 end_lineno = node.end_lineno - 1
357 col_offset = node.col_offset
358 end_col_offset = node.end_col_offset
359 except AttributeError:
360 return None
361
362 lines = _splitlines_no_ff(source)
363 if end_lineno == lineno:
364 return lines[lineno].encode()[col_offset:end_col_offset].decode()
365
366 if padded:
367 padding = _pad_whitespace(lines[lineno].encode()[:col_offset].decode())
368 else:
369 padding = ''
370
371 first = padding + lines[lineno].encode()[col_offset:].decode()
372 last = lines[end_lineno].encode()[:end_col_offset].decode()
373 lines = lines[lineno+1:end_lineno]
374
375 lines.insert(0, first)
376 lines.append(last)
377 return ''.join(lines)
378
379
380def walk(node):

Callers

nothing calls this directly

Calls 7

_splitlines_no_ffFunction · 0.85
_pad_whitespaceFunction · 0.85
decodeMethod · 0.45
encodeMethod · 0.45
insertMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected