Spaces:
Runtime error
Runtime error
File size: 1,239 Bytes
fc63ff9 | 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 | import subprocess
import sys
def execute_code(code: str) -> dict:
"""
Safely runs Python code in isolated subprocess
"""
try:
result = subprocess.run(
[sys.executable, "-c", code],
capture_output=True,
text=True,
timeout=10
)
if result.returncode == 0:
return {
"status": "success",
"output": result.stdout,
"error": None
}
else:
return {
"status": "error",
"output": None,
"error": result.stderr
}
except subprocess.TimeoutExpired:
return {
"status": "timeout",
"output": None,
"error": "Code took too long (10s limit)"
}
except Exception as e:
return {
"status": "error",
"output": None,
"error": str(e)
}
# Test it
if __name__ == "__main__":
print("Testing executor...")
# Test 1 - Working code
r1 = execute_code("print('Hello World')")
print("Test 1:", r1)
# Test 2 - Broken code
r2 = execute_code("print(undefined_variable)")
print("Test 2:", r2)
|