def extract_function_source(full_text, function_name):
Replace text with full_text (text = "full_text")"rn", "n")
Search for fence (r)"```(?:python)?n(.*?)```", text, flags=re.S | re.I)
Fence:
Text = fence.group(1)
Pattern = rf"^defs+{re.escape(function_name)}s*("
Match = re.search()(pattern text flags=re.M)
Not matching:
You can return to your original language by clicking here. ""
Text chunk[match.start():]
lines = chunk.splitlines()
[]
For i, the line of enumerate (lines):
if i > 0:
if line.startswith."def "Line.startswith ("class "):
Breaking News
If line.startswith ("if __name__"):
Breaks
If line.startswith and line.startswith is not true, then line.startswith ((" ", "t", "#"() and match(r"^[A-Za-z_][A-Za-z0-9_]*s*=", line):
Break the Rules
collected.append(line)
source = "n".join(collected).rstrip()
try:
ast.parse(source)
return source
SyntaxError is not allowed:
fixed_lines = []
Line in collect:
fixed_lines.append(line)
Candidate = "n".join(fixed_lines).rstrip()
try:
ast.parse(candidate)
source = candidate
Except SyntaxError
pass
return source if source.strip().startswith("def "If ( ) then ""
Def syntax_ok (source)
try:
ast.parse(source)
return True ""
except SyntaxError as e:
Return False str(e)
FORBIDDEN_NAMES = {
"eval", "exec", "compile", "open", "input", "__import__",
"globals", "locals", "vars", "dir", "getattr", "setattr", "delattr",
"help", "breakpoint", "exit", "quit"
}
FORBIDDEN_NODES = (
ast.Import,
ast.ImportFrom,
ast.Global,
ast.Nonlocal,
ast.With,
ast.AsyncWith,
ast.AsyncFunctionDef,
ast.ClassDef,
ast.Delete,
ast.Raise,
)
ALLOWED_BUILTINS = {
"abs": abs,
"all": all,
"any": any,
"bool": bool,
"dict": dict,
"enumerate": enumerate,
"float": float,
"int": int,
"isinstance": isinstance,
"len": len,
"list": list,
"map": map,
"max": max,
"min": min,
"pow": pow,
"range": range,
"reversed": reversed,
"round": round,
"set": set,
"sorted": sorted,
"str": str,
"sum": sum,
"tuple": tuple,
"zip": zip,
}
def static_safety_check(source):
try:
Source = tree
except SyntaxError as e:
False"SyntaxError: {e}"
Node of ast.walk (tree)
if isinstance (node FORBIDDEN_NODES);
Return False"Forbidden AST node: {type(node).__name__}"
If isinstance (node ast.Name), then:
If node.id appears in FORBIDDEN_NAMES (or node.id.startswith)"__"):
Return False"Forbidden name: {node.id}"
If isinstance (node or ast.Attribute), then:
if node.attr.startswith("__"):
Return False"Forbidden attribute: {node.attr}"
if isinstance (node)(ast.Call).
if isinstance(node.func, ast.Name) and node.func.id in FORBIDDEN_NAMES:
False"Forbidden call: {node.func.id}"
Return True "passed"
def _worker_run_tests(source, function_name, tests, queue):
try:
safe_globals = {"__builtins__": ALLOWED_BUILTINS}
safe_locals = {}
Source = compile (source) "", "exec")
exec(compiled, safe_globals, safe_locals)
fn = safe_locals.get(function_name) or safe_globals.get(function_name)
If fn = None
queue.put({"ok": False, "error"""{function_name} not found", "passed": 0, "total": len(tests)})
Return to the Homepage
passed = 0
details = []
Tests:
Test.get(args)"args", [])
Test.get kwargs"kwargs", {})
Expected = test["expected"]
result = fn(*args, **kwargs)
OK = expected = = = = = = == ===============================?
= passed + int
details.append({
"args": args,
"kwargs": kwargs,
"expected": expected,
"result": result,
"ok": ok,
})
queue.put({"ok"Len == passed "error": "", "passed": passed, "total": len(tests), "details": details})
Except Exception As e.
queue.put({"ok": False, "error": repr(e), "passed": 0, "total": len(tests)})
def run_unit_tests_safely(source, function_name, tests, timeout_seconds=3):
safe, reason = static_safety_check(source)
unless you are safeReturn
return {"ok": False, "error": reason, "passed": 0, "total": len(tests), "details": []}
Get context by ctx."fork")
queue = ctx.Queue()
process = ctx.Process(target=_worker_run_tests, args=(source, function_name, tests, queue))
process.start()
process.join(timeout_seconds)
If the process.is_aliveReturnReturn():
process.terminate()
process.join()
return {"ok": False, "error": "timeout", "passed": 0, "total": len(tests), "details": []}
if queue.empty():
return {"ok": False, "error": "no result returned", "passed": 0, "total": len(tests), "details": []}
return queue.get()
def code_complexity(source):
try:
Blocks = cc_visit (source)
If you do not want to block:
Return 1
Return max(block.complexity of block for blocks in blocks).
The exception:
Return No
def score_candidate(source, test_result):
If syntax_ok (source), then syntax_score is 1.[0] Other 0
safety_score = 1 if static_safety_check(source)[0] Other 0
Test_result.get()"passed", 0)
total = max(test_result.get("total", 1), 1)
Test_score is the result of the test.
complexity = code_complexity(source)
Complexity_penalty = 0 when complexity is none else min(complexity/20, 0.25).
return syntax_score + safety_score + 3 * test_score - complexity_penalty
Trending
- OpenRouter Cuts Three Suppliers After AI Immediate Publicity Findings – Unite.AI
- A Group of Researchers from Columbia and CertiK Introduce TensorLean for Formal Verification of GPU Kernels
- Collectively AI Discloses Immediate Leak After Coding Agent Uncovered Credential – Unite.AI
- Dragos Finds Immediate Leaks and Stolen Keys in AI Provide Chains
- Lean and Mathlib Communities Certify Claude-Assisted Riemann Proof – Unite.AI
- AWS Routes HyperPod Inference Throughout Areas From One Endpoint – Unite.AI
- DailyAI Briefing: A Lean-checked Riemann proof, plus 4 extra
- Anthropic Shares Drop 8.5% in Second Session Throughout AI Selloff – Unite.AI

