File size: 6,005 Bytes
abf246e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from typing import List, Tuple, Any, Optional
import re
import multiprocessing
from multiprocessing.connection import Connection
ExecuteResult = Tuple[bool, str, Tuple[bool]]
def extract_python_code(text_string: str) -> List[str]:
code_blocks = re.findall(r"```python(.*?)```", text_string, re.DOTALL)
if not code_blocks:
code_blocks = [text_string]
results = []
for block in code_blocks:
imports = re.findall(r"^(?:from\s+\S+\s+import\s+\S+|import\s+\S+.*)$", block, re.MULTILINE)
funcs = re.findall(r"(def\s+\w+\(.*?:[\s\S]*?)(?=^def\s|\Z)", block.strip(), re.MULTILINE)
if imports:
import_block = "\n".join(imports)
if funcs:
funcs = [import_block] + funcs
else:
funcs = [import_block]
results.extend(funcs)
return results
def rename_function(function: str, function_name: str) -> str:
"""
Replace the name of the first function in `answer` with `function_name`.
Only modifies the function name, keeps everything else intact.
"""
pattern = r"def\s+(\w+)\s*\("
new_answer = re.sub(pattern, f"def {function_name}(", function, count=1)
return new_answer
def _exec_code_and_capture(code: str, conn: Connection, work_dir: Optional[str] = None):
try:
if work_dir is not None:
os.makedirs(work_dir, exist_ok=True)
os.chdir(work_dir)
local_ns = {}
exec(code, local_ns)
for name, func in local_ns.items():
if callable(func) and name.startswith("test_"):
func()
conn.send(True)
except Exception as e:
conn.send(e)
finally:
conn.close()
class PyExecutor:
def _run_with_timeout(self, code: str, timeout: int, work_dir: Optional[str] = "./code_stuff") -> Any:
parent_conn, child_conn = multiprocessing.Pipe()
p = multiprocessing.Process(
target=_exec_code_and_capture,
args=(code, child_conn, work_dir)
)
p.start()
p.join(timeout)
if p.is_alive():
p.kill()
p.join()
raise TimeoutError("Test execution timed out")
if parent_conn.poll():
result = parent_conn.recv()
if isinstance(result, Exception):
raise result
return result
else:
raise RuntimeError("Child process terminated unexpectedly without sending a result.")
def execute(self, func: str, tests: List[str], timeout: int = 5, verbose: bool = True) -> ExecuteResult:
success_tests = []
failed_tests = []
is_passing = True
for test_code in tests:
cleaned_test = re.sub(r"^\s*from\s+solution\s+import\s+\w+\s*", "", test_code, flags=re.MULTILINE)
code_to_run = func + "\n" + cleaned_test
try:
self._run_with_timeout(code_to_run, timeout)
success_tests.append(test_code)
except Exception as e:
failed_tests.append(f"{test_code} # output: {e}")
is_passing = False
state = tuple(test in success_tests for test in tests)
feedback = (
"Tests passed:\n" + "\n".join(success_tests)
+ "\n\nTests failed:\n" + "\n".join(failed_tests)
)
return is_passing, feedback, state
def evaluate(self, name: str, func: str, test: str, timeout: int = 5) -> bool:
cleaned_test = re.sub(r"^\s*from\s+solution\s+import\s+\w+\s*", "", test, flags=re.MULTILINE)
code_to_run = func + "\n" + cleaned_test
try:
self._run_with_timeout(code_to_run, timeout)
return True
except Exception:
return False
def check_code_report(self, completions: list[str], tests: list[str], timeout: int = 5) -> tuple[list[str], list[float]]:
def extract_failed_tests(text: str) -> str:
match = re.search(r"Tests failed:\s*(.*)", text, re.DOTALL)
return match.group(1).strip() if match else ""
def extract_correct_function_name(text: str) -> str:
match = re.search(r"from\s+solution\s+import\s+([a-zA-Z_]\w*)", text)
return match.group(1) if match else ""
reports = []
avg_scores = []
for completion, test_code_str in zip(completions, tests):
func_blocks = extract_python_code(completion.strip())
collected_answer = '\n'.join(func_blocks)
correct_function_name = extract_correct_function_name(test_code_str)
if correct_function_name != "":
collected_answer = rename_function(collected_answer, correct_function_name)
test_block = extract_python_code(test_code_str.strip())
test_list = [test_block[0] + "\n\n" + block for block in test_block[1:]]
report_lines = []
success_examples = 0
for test in test_list:
func_name_match = re.search(r"def\s+(test_\w+)\s*\(", test)
func_name = func_name_match.group(1) if func_name_match else "unknown_test"
is_passing, feedback, _ = self.execute(collected_answer, [test], timeout=timeout)
if is_passing:
success_examples += 1
report_lines.append(f"✅ Test passed for '{func_name}'")
else:
report_lines.append(f"❌ Test failed for '{func_name}': \n{extract_failed_tests(feedback)}")
if len(test_list) != 0:
avg_score = success_examples / len(test_list)
else:
avg_score = 1.0
avg_scores.append(avg_score)
report_lines.append(f"\nAverage correctness: {avg_score:.2f}")
reports.append("\n".join(report_lines))
return reports, avg_scores
|