MCPcopy Index your code
hub / github.com/evalplus/evalplus / insert_contract

Function insert_contract

evalplus/perf/sampling.py:22–55  ·  view source on GitHub ↗
(entry_point: str, code: str, contract: str)

Source from the content-addressed store, hash-verified

20
21# this is more of a hack... rather than a "verified" implementation
22def insert_contract(entry_point: str, code: str, contract: str):
23 # why is this so complicated? because the contract might be mis-indented...
24 def get_first_indent_size(source, body_char_start_idx):
25 assert source.strip()
26 indent_size = 0
27 while source[body_char_start_idx - indent_size - 1] == " ":
28 indent_size += 1
29 return indent_size
30
31 code = code.replace("\t", " " * 4)
32 contract = contract.replace("\t", " " * 4)
33
34 lines = [line for line in code.split("\n") if line.strip()]
35 fn_def_line = [line for line in lines if line.startswith(f"def {entry_point}")][0]
36 def_line_idx = lines.index(fn_def_line)
37 body_start_idx = code.index(code.split(fn_def_line)[1].lstrip())
38
39 source_indent: int = get_first_indent_size(code, body_start_idx)
40 contract_indent: int = get_first_indent_size(
41 contract, len(contract) - len(contract.lstrip())
42 )
43 return "\n".join(
44 lines[: def_line_idx + 1]
45 + [
46 " " * max(0, source_indent - contract_indent) + cline
47 for cline in contract.split("\n")
48 if cline
49 ]
50 + [
51 " " * max(0, contract_indent - source_indent) + sline
52 for sline in lines[def_line_idx + 1 :]
53 if sline
54 ]
55 )
56
57
58def post_process(text: str) -> Optional[str]:

Callers 1

mainFunction · 0.85

Calls 1

get_first_indent_sizeFunction · 0.85

Tested by

no test coverage detected