MCPcopy Index your code
hub / github.com/RustPython/RustPython / getcomments

Function getcomments

Lib/inspect.py:1009–1052  ·  view source on GitHub ↗

Get lines of comments immediately preceding an object's source code. Returns None when source can't be found.

(object)

Source from the content-addressed store, hash-verified

1007 raise OSError('could not find code object')
1008
1009def getcomments(object):
1010 """Get lines of comments immediately preceding an object's source code.
1011
1012 Returns None when source can't be found.
1013 """
1014 try:
1015 lines, lnum = findsource(object)
1016 except (OSError, TypeError):
1017 return None
1018
1019 if ismodule(object):
1020 # Look for a comment block at the top of the file.
1021 start = 0
1022 if lines and lines[0][:2] == '#!': start = 1
1023 while start < len(lines) and lines[start].strip() in ('', '#'):
1024 start = start + 1
1025 if start < len(lines) and lines[start][:1] == '#':
1026 comments = []
1027 end = start
1028 while end < len(lines) and lines[end][:1] == '#':
1029 comments.append(lines[end].expandtabs())
1030 end = end + 1
1031 return ''.join(comments)
1032
1033 # Look for a preceding block of comments at the same indentation.
1034 elif lnum > 0:
1035 indent = indentsize(lines[lnum])
1036 end = lnum - 1
1037 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
1038 indentsize(lines[end]) == indent:
1039 comments = [lines[end].expandtabs().lstrip()]
1040 if end > 0:
1041 end = end - 1
1042 comment = lines[end].expandtabs().lstrip()
1043 while comment[:1] == '#' and indentsize(lines[end]) == indent:
1044 comments[:0] = [comment]
1045 end = end - 1
1046 if end < 0: break
1047 comment = lines[end].expandtabs().lstrip()
1048 while comments and comments[0].strip() == '#':
1049 comments[:1] = []
1050 while comments and comments[-1].strip() == '#':
1051 comments[-1:] = []
1052 return ''.join(comments)
1053
1054class EndOfBlock(Exception): pass
1055

Callers

nothing calls this directly

Calls 9

findsourceFunction · 0.85
ismoduleFunction · 0.85
lenFunction · 0.85
indentsizeFunction · 0.85
stripMethod · 0.45
appendMethod · 0.45
expandtabsMethod · 0.45
joinMethod · 0.45
lstripMethod · 0.45

Tested by

no test coverage detected