-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexecute_code.py
33 lines (26 loc) · 902 Bytes
/
execute_code.py
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
import sys
import subprocess
import shlex
def exec_code(code, python_interpreter=None):
code = "import os\n" + code
if python_interpreter is None:
python_executable = sys.executable
else:
python_executable = python_interpreter
command = [python_executable, "-c", code]
try:
result = subprocess.run(command, capture_output=True, text=True, timeout=85)
if result.returncode != 0:
output = result.stderr.strip()
error = True
else:
output = result.stdout.strip()
error = False
if not output:
output = ""
error = False
return {"output": output, "error": error}
except subprocess.TimeoutExpired:
return {"output": "Execution timed out.", "error": True}
except Exception as e:
return {"output": str(e), "error": True}