forked from OpenBMB/ChatDev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_executor.py
More file actions
executable file
·66 lines (56 loc) · 2.03 KB
/
code_executor.py
File metadata and controls
executable file
·66 lines (56 loc) · 2.03 KB
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
def execute_code(code: str, time_out: int = 60) -> str:
"""
Execute code and return std outputs and std error.
Args:
code (str): Code to execute.
time_out (int): time out, in second.
Returns:
str: std output and std error
"""
import os
import sys
import subprocess
import uuid
from pathlib import Path
def __write_script_file(_code: str):
_workspace = Path(os.getenv('TEMP_CODE_DIR', 'temp')).resolve()
_workspace.mkdir(exist_ok=True)
filename = f"{uuid.uuid4()}.py"
code_path = _workspace / filename
code_content = _code if _code.endswith("\n") else _code + "\n"
code_path.write_text(code_content, encoding="utf-8")
return code_path
def __default_interpreter() -> str:
return sys.executable or "python3"
script_path = None
stdout = ""
stderr = ""
try:
script_path = __write_script_file(code)
workspace = script_path.parent
cmd = [__default_interpreter(), str(script_path.resolve())]
try:
completed = subprocess.run(
cmd,
cwd=str(workspace),
capture_output=True,
timeout=time_out,
check=False
)
stdout = completed.stdout.decode('utf-8', errors="replace")
stderr = completed.stderr.decode('utf-8', errors="replace")
except subprocess.TimeoutExpired as e:
stdout = e.stdout.decode('utf-8', errors="replace") if e.stdout else ""
stderr = e.stderr.decode('utf-8', errors="replace") if e.stderr else ""
stderr += f"\nError: Execution timed out after {time_out} seconds."
except Exception as e:
stderr = f"Execution error: {str(e)}"
except Exception as e:
stderr = f"Setup error: {str(e)}"
finally:
if script_path and script_path.exists():
try:
os.remove(script_path)
except Exception:
pass
return stdout + stderr