MCPcopy Index your code
hub / github.com/python/cpython / getblock

Function getblock

Lib/inspect.py:1139–1156  ·  view source on GitHub ↗

Extract the block of code at the top of the given list of lines.

(lines)

Source from the content-addressed store, hash-verified

1137 raise EndOfBlock
1138
1139def getblock(lines):
1140 """Extract the block of code at the top of the given list of lines."""
1141 blockfinder = BlockFinder()
1142 try:
1143 tokens = tokenize.generate_tokens(iter(lines).__next__)
1144 for _token in tokens:
1145 blockfinder.tokeneater(*_token)
1146 except (EndOfBlock, IndentationError):
1147 pass
1148 except SyntaxError as e:
1149 if "unmatched" not in e.msg:
1150 raise e from None
1151 _, *_token_info = _token
1152 try:
1153 blockfinder.tokeneater(tokenize.NEWLINE, *_token_info)
1154 except (EndOfBlock, IndentationError):
1155 pass
1156 return lines[:blockfinder.last]
1157
1158def getsourcelines(object):
1159 """Return a list of source lines and starting line number for an object.

Callers 1

getsourcelinesFunction · 0.85

Calls 2

tokeneaterMethod · 0.95
BlockFinderClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…