Spaces:
Sleeping
Sleeping
File size: 6,290 Bytes
aaa634c | 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | import sys
import subprocess
import json
import tempfile
import os
EXECUTION_TEMPLATE = """
import sys
import json
import traceback
# --- User Code Start ---
{user_code}
# --- User Code End ---
def run_tests():
test_cases = {test_cases_json}
results = []
# Locate entry point function
func = None
if 'solve' in globals() and callable(globals()['solve']):
func = globals()['solve']
else:
for name, obj in list(globals().items()):
if callable(obj) and not name.startswith('_') and name not in ('run_tests', 'sys', 'json', 'traceback', 'copy'):
func = obj
break
if not func:
print(json.dumps([ {{"status": "error", "error": "No callable function found in your code. Please define a function (e.g., 'def solve(...)')."}} ]))
return
for idx, tc in enumerate(test_cases):
inputs = tc.get("input", [])
expected = tc.get("expected")
if not isinstance(inputs, list):
inputs = [inputs]
try:
import copy
args = copy.deepcopy(inputs)
res = func(*args)
# Comparison
is_match = (res == expected)
results.append({{
"index": idx,
"status": "passed" if is_match else "failed",
"input": inputs,
"expected": expected,
"got": res
}})
except Exception as e:
results.append({{
"index": idx,
"status": "error",
"input": inputs,
"expected": expected,
"error": str(e),
"traceback": traceback.format_exc().splitlines()[-2:]
}})
print(json.dumps(results))
if __name__ == '__main__':
run_tests()
"""
def run_python_sandbox(user_code: str, test_cases: list, timeout: float = 4.0) -> dict:
"""
Executes user code against test cases in an isolated sandbox.
Attempts Docker execution first, then falls back to restricted subprocess.
"""
test_cases_json = json.dumps(test_cases)
full_script = EXECUTION_TEMPLATE.format(
user_code=user_code,
test_cases_json=test_cases_json
)
# Try Docker execution
try:
# Run python:3.11-slim container with stdin, no network, 128MB memory, 0.5 CPU
# On Windows, we pipe the script directly to python via stdin to avoid path mounts
cmd = [
"docker", "run", "--rm", "-i",
"--network", "none",
"--memory", "128m",
"--cpus", "0.5",
"python:3.11-slim",
"python"
]
proc = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = proc.communicate(input=full_script, timeout=timeout)
if proc.returncode == 137:
return {
"success": False,
"error": "Memory limit exceeded (OOM) or container killed.",
"sandbox_type": "docker"
}
if proc.returncode != 0:
return {
"success": False,
"error": stderr or f"Execution failed with code {proc.returncode}",
"sandbox_type": "docker"
}
try:
results = json.loads(stdout.strip())
return {
"success": True,
"results": results,
"sandbox_type": "docker"
}
except json.JSONDecodeError:
return {
"success": False,
"error": f"Invalid output from execution: {stdout}\nErrors: {stderr}",
"sandbox_type": "docker"
}
except (subprocess.TimeoutExpired, TimeoutError):
# Clean up process
try:
proc.kill()
except Exception:
pass
return {
"success": False,
"error": f"Execution timed out. Code exceeded max runtime of {timeout}s.",
"sandbox_type": "docker"
}
except Exception as docker_err:
# Fall back to native local subprocess execution with limited environment
return run_local_fallback(full_script, timeout, str(docker_err))
def run_local_fallback(script_content: str, timeout: float, debug_msg: str) -> dict:
"""
Fallback execution using current python environment.
"""
try:
proc = subprocess.Popen(
[sys.executable],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = proc.communicate(input=script_content, timeout=timeout)
if proc.returncode != 0:
return {
"success": False,
"error": stderr or f"Local execution failed with code {proc.returncode}",
"sandbox_type": "local_fallback",
"sandbox_warning": f"Docker isolation unavailable: {debug_msg}"
}
try:
results = json.loads(stdout.strip())
return {
"success": True,
"results": results,
"sandbox_type": "local_fallback",
"sandbox_warning": f"Docker isolation unavailable: {debug_msg}"
}
except json.JSONDecodeError:
return {
"success": False,
"error": f"Invalid output from local runner: {stdout}",
"sandbox_type": "local_fallback"
}
except subprocess.TimeoutExpired:
try:
proc.kill()
except Exception:
pass
return {
"success": False,
"error": f"Local execution timed out. Code exceeded {timeout}s.",
"sandbox_type": "local_fallback"
}
except Exception as e:
return {
"success": False,
"error": f"Failed to execute local fallback runner: {str(e)}",
"sandbox_type": "none"
}
|