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

Function extract_target_code_or_empty

evalplus/sanitize.py:110–165  ·  view source on GitHub ↗
(code: str, entrypoint: Optional[str] = None)

Source from the content-addressed store, hash-verified

108
109
110def extract_target_code_or_empty(code: str, entrypoint: Optional[str] = None) -> str:
111 code = code_extract(code)
112 code_bytes = bytes(code, "utf8")
113 parser = Parser(Language(tree_sitter_python.language()))
114 tree = parser.parse(code_bytes)
115 class_names = set()
116 function_names = set()
117 variable_names = set()
118
119 root_node = tree.root_node
120 import_nodes = []
121 definition_nodes = []
122
123 for child in root_node.children:
124 if child.type in IMPORT_TYPE:
125 import_nodes.append(child)
126 elif child.type == CLASS_TYPE:
127 name = get_definition_name(child)
128 if not (
129 name in class_names or name in variable_names or name in function_names
130 ):
131 definition_nodes.append((name, child))
132 class_names.add(name)
133 elif child.type == FUNCTION_TYPE:
134 name = get_definition_name(child)
135 if not (
136 name in function_names or name in variable_names or name in class_names
137 ) and has_return_statement(child):
138 definition_nodes.append((name, child))
139 function_names.add(get_definition_name(child))
140 elif (
141 child.type == EXPRESSION_TYPE and child.children[0].type == ASSIGNMENT_TYPE
142 ):
143 subchild = child.children[0]
144 name = get_definition_name(subchild)
145 if not (
146 name in variable_names or name in function_names or name in class_names
147 ):
148 definition_nodes.append((name, subchild))
149 variable_names.add(name)
150
151 if entrypoint:
152 name2deps = get_deps(definition_nodes)
153 reacheable = get_function_dependency(entrypoint, name2deps)
154
155 sanitized_output = b""
156
157 for node in import_nodes:
158 sanitized_output += code_bytes[node.start_byte : node.end_byte] + b"\n"
159
160 for pair in definition_nodes:
161 name, node = pair
162 if entrypoint and not (name in reacheable):
163 continue
164 sanitized_output += code_bytes[node.start_byte : node.end_byte] + b"\n"
165 return sanitized_output[:-1].decode("utf8")
166
167

Callers 1

sanitizeFunction · 0.85

Calls 5

code_extractFunction · 0.85
get_definition_nameFunction · 0.85
has_return_statementFunction · 0.85
get_depsFunction · 0.85
get_function_dependencyFunction · 0.85

Tested by

no test coverage detected