File size: 2,126 Bytes
46300b3 | 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 | """
Code verifier: extract a python code block from completion, run it in a
subprocess with the task's test cases appended, with a hard timeout.
NEVER use in-process exec(). Subprocess only.
"""
from __future__ import annotations
import re
import subprocess
import sys
import tempfile
from pathlib import Path
from typing import Any, Dict
_CODE_BLOCK = re.compile(r"```(?:python)?\s*\n(.*?)```", re.DOTALL | re.IGNORECASE)
def _extract_code(completion: str) -> tuple[str | None, bool]:
if not completion:
return None, False
m = _CODE_BLOCK.search(completion)
if m:
return m.group(1).strip(), True
if "def " in completion:
return completion.strip(), False
return None, False
def verify_code(task: Dict[str, Any], completion: str) -> Dict[str, Any]:
code, format_ok = _extract_code(completion or "")
if code is None:
return {"correctness": 0.0, "format_ok": False, "details": "no code"}
tests = task.get("tests", [])
if not tests:
return {"correctness": 0.0, "format_ok": format_ok, "details": "no tests in task"}
test_block = "\n".join(tests)
program = f"{code}\n\n# --- tests ---\n{test_block}\nprint('__OK__')\n"
with tempfile.TemporaryDirectory() as td:
path = Path(td) / "candidate.py"
path.write_text(program, encoding="utf-8")
try:
proc = subprocess.run(
[sys.executable, str(path)],
capture_output=True,
text=True,
timeout=5,
)
except subprocess.TimeoutExpired:
return {"correctness": 0.0, "format_ok": format_ok, "details": "timeout"}
except Exception as e:
return {"correctness": 0.0, "format_ok": format_ok, "details": f"runner err: {e}"}
ok = proc.returncode == 0 and "__OK__" in (proc.stdout or "")
detail = (proc.stderr or proc.stdout or "")[:200].replace("\n", " ")
return {
"correctness": 1.0 if ok else 0.0,
"format_ok": format_ok,
"details": detail,
}
|